Subject Re: Sum results of a subquery?
Author Svein Erling Tysvær
--- In firebird-support@yahoogroups.com, Kjell Rilbe wrote:
> I guess it comes down to choosing between one of two performance
> hits:
>
> 1. doing a semi-compiled loop in SP language or SQL (using a view as
> per Alan's suggestion) compared to a truly compiled loop in the
> application, or
>
> 2. transferring more data over a local TCP/IP connection.

In your case, there is actually a 3. only involving SQL as well:

select count(*)
from "TheTable" T1
join "TheTable" T2
on T2."Category" = T1."Category" and T2.PK > T1.PK
where not exists(select * from "TheTable" T3
where T3."Category" = T1."Category" and T3.PK < T1.PK)

PK is PrimaryKey or anything unique within "TheTable". The NOT EXISTS
will ascertain that T1 only contains the first record for each
category, whereas T2.PK > T1.PK will take make T2 contain all records
but the first for each category.

Though I guess this become messy once you use count>2 rather than >1
(it should still be possible) and I have no idea about performance.

Set