Subject Re: help on query
Author Svein Erling
--- In firebird-support@yahoogroups.com, "Leyne, Sean" wrote:
> Sugheer,
>
> > I have a table data like this
> ...
> > Either debit or credit will have only one entry.
>
> Although you could write a SELECT statement, to be honest the better
> solution would be to change you table schema to add a new column to
> 'mark' the details rows you need to report on. Is that an option?
>
> A SELECT statement would perform VERY VERY POORLY.

Well, I think this depends on what kind of assumptions that can be
made. From his provided data, I'd say that simply doing

select account_id
from table1 t1
where not exists(
select * from table1 t2
where t2.journal_no = t1.journal_no and
(t2.debit > t1.debit or t2.credit > t1.credit))

could give him the desired result (a sum of positive values will
always be at least as large as any individual item) and such a query
should not be too slow if there is an index on journal_no and that
field does not contain lots of duplicates. Of course, if there is only
one individual item (in addition to the sum) for a journal_no, then we
do not know how to separate the sum from the item and my suggested
query would return two rows.

Set