Top N per group query in Vertica

China☆狼群 提交于 2019-12-06 02:42:36

Let's set up the sample data:

CREATE TABLE public.test (
  A int,
  B int,
  C varchar,
  D int, 
  E int
);

INSERT INTO public.test (A, B, C, D, E) VALUES (1, 2, 'AAA', 4, 1404305559);
INSERT INTO public.test (A, B, C, D, E) VALUES (1, 2, 'BBB', 23, 1404305633);
INSERT INTO public.test (A, B, C, D, E) VALUES (1, 2, 'CCC', 62, 1404305705);
INSERT INTO public.test (A, B, C, D, E) VALUES (1, 2, 'AAA', 123, 1404305740);
INSERT INTO public.test (A, B, C, D, E) VALUES (1, 2, 'BBB', 91, 1404305778);

COMMIT;

We'll use the RANK function to rank each row based on A, B, C and sort on E and return only the rows that are at the top (have a rank of 1).

SELECT a.a, 
       a.b, 
       a.c, 
       a.d, 
       a.e 
FROM   (SELECT a, 
               b, 
               c, 
               d, 
               e, 
               RANK() 
                 OVER ( 
                   PARTITION BY a, b, c 
                   ORDER BY e DESC) AS rank 
        FROM   public.test) a 
WHERE  a.rank = 1; 

This returns:

 A | B |  C  |  D  |     E
---+---+-----+-----+------------
 1 | 2 | CCC |  62 | 1404305705
 1 | 2 | AAA | 123 | 1404305740
 1 | 2 | BBB |  91 | 1404305778
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!