Subject Re: Help with query please
Author Svein Erling Tysvær
Hi Adele!

This query may or may not be what you want:

SELECT A.UniqueID, A.MyLength
FROM MyTable A
WHERE (SELECT SUM(B.MyLength) FROM MyTable B
WHERE B.SomeField = A.SomeField) < 500

I added the SomeField because you would most likely want to check if
the sum is < 500 for certain groups of records. If you really only
want to return all records if the sum of MyLength for the entire table
is < 500, then you may simply remove that part. Depending on indexes
etc. this query may be slow, but you do not say anything about this
table containing millions of records...

HTH,
Set

--- In firebird-support@yahoogroups.com, "adele_leroux" wrote:
> Hi All, I would like to know if it is possible to write a select
> query to be able to retrieve records from a single table where the
> sum of the records does not exceed a certain amount. I know this
> query is not going to work, but just to explain my problem to you I
> will use this as an example...
> SELECT UniqueID, MyLength
> FROM MyTable
> WHERE SUM( MyLength ) < 500
>
> The result should be multiple records where the sum of all the
> records returned does not exceed 500. If this is not possible by
> using a select query, I would appreciate any input on how to
> accomplish this without retrieving all the records from the
> database.
>
> Thanks in advance,
> Adele