Subject Re: [ib-support] sum(field) with subselect
Author Svein Erling Tysvaer
Uhum,
I think the subselect is executed for every row in your table, though I
don't know why this causes trouble. I assume that you do have an order by
clause in your original subselect, or is it supposed to be random which
records are returned?

You could try something like

select sum(field) from Table T1
where not exists (select 1 from Table T2 where T2.ID < T1.ID and
exists(Select 1 from Table T3 where T3.ID < T2.ID))

This would eliminate all records but those having the two smallest values
of ID.

HTH,
Set

At 14:22 07.09.2002 +0200, you wrote:
>Example:
>I need sum two rows in table, but those rows is returned from
>subselect.
>
>select sum(Field) from Table where ID in (select first 2 ID from Table);
>
>This statement tot up all rows from Table and not only two rows that
>were returned from subselect.