Re: TOP 100 in SQL
Posted in 1998
This seems to work:
create temp table t ( A char(10),
sum_B integer,
seq_num serial ) with no log ;
select A, sum(B) sum_B
from XXX
group by A
order by sum_B desc
into temp p with no log ;
insert into t ( A, sum_B )
select A, sum_B
from p ;
select A, sum_B
from t
where seq_num <= 10 ;
and doesn't require the "first 10" syntax.
Make t.A the same datatype as XXX.A,
and t.sum_B the same datatype as XXX.B
It depends on the fact that rows get pulled out of the temp table "p"
in the same order in which they were inserted, which seems to be the
case but I'm not sure that it is guaranteed.
There's probably a much better way to do it, but I've had only two cups
of coffee so far this morning...
- Paul