简体   繁体   中英

FInding the the most frequent value for a particular year in postgres

In postgre, I want to find the user(s) who has the highest number of "opened" requests in the year 2011 for the following database. I am new to postgre and I am stuck with this.


ID  PERSON      REQUEST   DATE
4   datanoise   opened  2008-09-02 
5   marsuboss   opened  2009-09-02 
6   m3talsmith  opened  2009-09-06 
7   sferik  opened  2010-09-08 
8   sferik  opened  2010-09-09 
8   dtrasbo discussed   2010-09-09 
8   brianmario  discussed   2011-09-09 
8   sferik  discussed   2011-09-09 
9   rsim    opened  2011-09-09 
.....more tuples to follow

this is just a small part of the database, there are around 250,000 tuples in total.

You can't nest aggregates, and in any case you need the whole top row ordered by count:

select person, count(request) as countRequests
from table
where (date >= '2011-01-01' and date < '2012-01-01')
    and request = 'opened'
group by person
order by countRequests desc
limit 1;

The best way to filter by date is to use a half-open interval ie >= start and < dayAfterEnd

Could you please try the following

 WITH CTE(ID,PERSON,REQUEST,DATE) AS
  (
    SELECT 4,'datanoise','opened','2008-09-02'
       UNION ALL  
    SELECT 5,   'marsuboss',   'opened',  '2009-09-02' 
       UNION ALL
    SELECT 6 ,  'm3talsmith',  'opened' , '2009-09-06' 
       UNION ALL
    SELECT 7 ,  'sferik' , 'opened',  '2010-09-08' 
       UNION ALL
    SELECT 8 ,  'sferik',  'opened' , '2010-09-09' 
       UNION ALL
    SELECT 8 ,  'dtrasbo', 'discussed',   '2010-09-09' 
      UNION ALL
    SELECT 8 ,  'brianmario',  'discussed' ,  '2011-09-09' 
       UNION ALL
    SELECT 8 ,  'sferik',  'discussed',   '2011-09-09' 
       UNION ALL
    SELECT 9 ,  'rsim',    'opened',  '2011-09-09' 
   )
   SELECT C.PERSON,COUNT(C.ID)CNTT
      FROM CTE AS C
      WHERE C.DATE >= '2011-01-01'AND  C.DATE<'2012-01-01'
         AND C.REQUEST='OPENED'
      GROUP BY C.PERSON 
      ORDER BY COUNT(C.ID)DESC
      LIMIT 1;

You are looking for more than one person. That suggests using window functions:

SELECT P.*
FROM (SELECT T.PERSON, COUNT(*) AS CNT,
             RANK() OVER (OVER BY COUNT(*) DESC) AS SEQNUM
      FROM T
      WHERE T.DATE >= '2011-01-01'AND T.DATE < '2012-01-01' AND
            T.REQUEST = 'OPENED'
      GROUP BY T.PERSON 
     ) P
WHERE seqnum = 1;

Or if you are using Postgres 13+, you can use WITH TIES :

SELECT T.PERSON, COUNT(*) AS CNT
FROM T
WHERE T.DATE >= '2011-01-01'AND T.DATE < '2012-01-01' AND
      T.REQUEST = 'OPENED'
GROUP BY T.PERSON
ORDER BY COUNT(*) DESC
FETCH FIRST 1 ROW WITH TIES

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM