sql/presto:如何查找每一天之前存在但不是当天的记录
sql/presto: how to find records that exist before but not current day for each day
我有如下数据:
item day
a 1
b 1
c 1
a 2
c 2
d 2
a 3
d 3
并且期望的结果是为每一天找到在这一天之前消失的记录。期望的结果:
disappeared_item day_of_interest
b 2 (as of the second day, only b disappeared)
b,c 3 (as of the fourth day, b and c disappeared)
我想找出每个项目的最大(天)数,但在如何获得每天累积的项目方面得到了堆栈
WITH
items AS ( SELECT DISTINCT item
FROM test ),
days AS ( SELECT DISTINCT day
FROM test )
SELECT GROUP_CONCAT(items.item) disappeared_item,
days.day day_of_interest
FROM items
CROSS JOIN days
WHERE EXISTS ( SELECT NULL
FROM test
WHERE test.item = items.item
AND test.day < days.day )
AND NOT EXISTS ( SELECT NULL
FROM test
WHERE test.item = items.item
AND test.day = days.day )
GROUP BY days.day;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c35dbe8b9b0a0e00f69c542a70be4bfc
我有如下数据:
item day
a 1
b 1
c 1
a 2
c 2
d 2
a 3
d 3
并且期望的结果是为每一天找到在这一天之前消失的记录。期望的结果:
disappeared_item day_of_interest
b 2 (as of the second day, only b disappeared)
b,c 3 (as of the fourth day, b and c disappeared)
我想找出每个项目的最大(天)数,但在如何获得每天累积的项目方面得到了堆栈
WITH
items AS ( SELECT DISTINCT item
FROM test ),
days AS ( SELECT DISTINCT day
FROM test )
SELECT GROUP_CONCAT(items.item) disappeared_item,
days.day day_of_interest
FROM items
CROSS JOIN days
WHERE EXISTS ( SELECT NULL
FROM test
WHERE test.item = items.item
AND test.day < days.day )
AND NOT EXISTS ( SELECT NULL
FROM test
WHERE test.item = items.item
AND test.day = days.day )
GROUP BY days.day;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=c35dbe8b9b0a0e00f69c542a70be4bfc