我正在努力寻找最好的用户在每年的每个月基于位置
我使用以下数据
user_id location date Product_brought
........ ......... ..... ...............
1 ks 2017-08-17 prod1
1 ks 2017-08-17 prod2
2 ks 2017-08-17 prod1
2 ks 2017-01-17 prod5
2 ks 2017-01-17 prod4
1 ks 2017-01-17 prod2
4 Tg 2017-08-17 prod4
5 Tg 2017-08-17 prod5
5 Tg 2017-08-17 prod7
5 Tg 2017-08-17 prod4
5 Tg 2017-01-17 prod3
6 Tg 2017-01-17 prod4
6 Tg 2017-01-17 prod3
6 Tg 2017-08-17 prod1
1 ks 2017-08-17 prod1
1 ks 2017-08-17 prod2
2 ks 2017-08-17 prod1
2 ks 2017-01-17 prod5
2 ks 2017-01-17 prod4
1 ks 2017-01-17 prod2
4 Tg 2017-08-17 prod4
5 Tg 2017-08-17 prod5
5 Tg 2017-08-17 prod7
5 Tg 2017-08-17 prod4
5 Tg 2017-01-17 prod3
6 Tg 2017-01-17 prod4
6 Tg 2017-01-17 prod3
6 Tg 2017-08-17 prod1
我期待以下输出
user year month location count
..... .... ...... ......... ......
1 2017 8 ks 2
2 2017 1 ks 2
5 2017 8 Tg 3
6 2017 1 Tg 2
1 2016 8 ks 2
2 2016 1 ks 2
5 2016 8 Tg 3
6 2016 1 Tg 2
我尝试使用以下查询
SELECT ds.*
FROM (SELECT user,month(date),year(date), location, count(product_brought) as c,
ROW_NUMBER() OVER (PARTITION BY month(date),year(date) ORDER BY COUNT(product) DESC) as seqnum
FROM table
GROUP BY month(date), year(date),location, user
) ds
WHERE seqnum = 1
ORDER BY c DESC ;
它没有得到预期的输出
1条答案
按热度按时间dffbzjpn1#