我有一个包含四列的表格,如下所示:
id,name, key1, key2, key3, date
1,'A' ,'x1','y1','z1','2015-11-11'
2,'A' ,'x1','y2','z1','2015-11-11'
3,'B' ,'x1','y2','z1','2015-11-11'
4,'B' ,'x1','y1','z2','2015-11-11'
5,'A' ,'x1','y1','z1','2015-11-12'
6,'A' ,'x1','y2','z3','2015-11-12'
7,'B' ,'x2','y3','z3','2015-11-12'
8,'B' ,'x2','y1','z4','2015-11-12'
9,'D' ,'x2','y1','z4','2015-11-12'
我想计算每个key1, key2, key3
的不同新 date
- s的数量:
2015-11-11 3 -- (two distinct key1, key2, key3 row 2,3)
2015-11-12 3 -- (new key and distinct key1, key2, key3)
我该怎么做?
答案 0 :(得分:1)
假设您的钥匙没有|在他们里面的char(如果不是这样的话,选择另一个char:
SELECT DATE, EXACT_COUNT_DISTINCT(key) AS keys
FROM (
SELECT DATE, key, LEAD(DATE) OVER(PARTITION BY key ORDER BY DATE DESC) AS new
FROM (SELECT DATE, CONCAT(key1, '|', key2, '|', key3) AS key
FROM YourTable GROUP BY 1,2)
) WHERE new IS NULL
GROUP BY DATE
ORDER BY DATE
您可以注意到,这只是我对您之前问题的回答。我将所有三个键合并为一个 - 并应用与以前完全相同的代码