[英]SQL Group by only correlative rows
Say I have the following table: 说我有下表:
Code A B C Date ID
------------------------------
50 1 1 A 2018-01-08 150001
50 1 1 A 2018-01-15 165454
50 1 1 B 2018-02-01 184545
50 1 1 A 2018-02-02 195487
I need the sql query to output the following: 我需要sql查询来输出以下内容:
Code A B C Min(Date) Min(ID)
-------------------------------
50 1 1 A 2018-01-08 150001
50 1 1 B 2018-02-01 184545
50 1 1 A 2018-02-02 195487
If I use standard group by, rows 1,2,4 are grouped in 1 row, and this is not that I want. 如果我使用标准分组依据,则行1,2,4被分组为1行,这不是我想要的。 I want to select the row with MIN(date) and MIN(id) from the duplicate records that are together based on column code, A, B and C in this case 1st 2 rows are duplicates so i want the min() row.
我想从基于列代码,A,B和C的重复记录中选择具有MIN(date)和MIN(id)的行,在这种情况下,第一行和第二行是重复的,所以我想要min()行。 and 3rd and 4th row are distinct.
第三行和第四行是不同的。
Note that the database is Vertica 8.1, that is very similar to Oracle or PostgreSQL 请注意,数据库为Vertica 8.1,与Oracle或PostgreSQL非常相似
I think you would need the analytic function LAG() . 我认为您将需要解析函数LAG() 。 Using this function, you can get the value of the previous row (or NULL if it's the first row itself).
使用此函数,您可以获得上一行的值(如果它是第一行本身,则为NULL)。 So you can check if the value on the previous row is different or not, and filter accordingly.
因此,您可以检查上一行的值是否不同,然后进行相应过滤。
I'm not familiar with Vertica, but this should be the correct documentation for it: https://my.vertica.com/docs/7.0.x/HTML/Content/Authoring/SQLReferenceManual/Functions/Analytic/LAGAnalytic.htm 我不熟悉Vertica,但这应该是正确的文档: https : //my.vertica.com/docs/7.0.x/HTML/Content/Authoring/SQLReferenceManual/Functions/Analytic/LAGAnalytic.htm
Please try the query below, it should do it: 请尝试下面的查询,它应该这样做:
SELECT l.Code, l.A, l.B, l.C, l.Date, l.ID
FROM (SELECT t.*,
LAG(t.C, 1) OVER (PARTITION BY t.Code, t.A ORDER BY t.Date) prev_val
FROM table_1 t) l
WHERE l.C != l.prev_val
OR l.prev_val IS NULL
ORDER BY l.Code, l.A, l.Date
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.