我有数据:
user_id user_login_date
1 2013.07.05
1 2013.07.15
1 2013.07.16
1 2013.07.17
2 2013.07.05
2 2013.07.05
2 2013.07.15
我想制作如下所示的虚拟表:
user_id user_login_date date_id
1 2013.07.05 1
1 2013.07.15 2
1 2013.07.16 3
1 2013.07.17 4
2 2013.07.05 1
2 2013.07.05 2
2 2013.07.15 3
我怎么做?我试过:
WITH user_count
AS (
SELECT user_id, user_login_date
FROM users
)
SELECT user_count.user_id, user_count.user_login_date, COUNT(user_count.user_id)
FROM users, user_count
WHERE users.user_login_date >= user_count.user_login_date
AND users.user_id = user_count.user_id
GROUP BY user_count.user_id, user_count.user_login_date
ORDER BY user_count.user_id, user_count.user_login_date;
但结果不是我想要的。