Search Postgresql Archives

Top N within groups?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



[was {SOLVED?] Re: [GENERAL] functional index not used, looping simpler query just faster]
Ivan Sergio Borgonovo wrote:
I'm still curious to know if this could be done
efficiently with just one query.
[thinking out loud]
Can someone familiar with the source for DISTINCT ON comment on how hard it would be to add another parameter to return more than one row?
e.g.
To do TOP 1 within an unknown number of groups
  select distinct on (groupid) groupid, identifier, count(*)
  from somequery
  group by groupid, identifier
  order by 1,3 desc,2;

I'm thinking that, for the top 3 within each group, something like
  select distinct on (groupid) FOR 3 groupid, identifier, count(*)
  from somequery
  group by groupid, identifier
  order by 1,3 desc,2;

For Ivan's case, groupid = brand, identifer = item. The where clause applies the date limits.

klint.

--
Klint Gore
Database Manager
Sheep CRC
A.G.B.U.
University of New England
Armidale NSW 2350

Ph: 02 6773 3789 Fax: 02 6773 3266
EMail: kgore4@xxxxxxxxxx



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux