我有以下表格:
内容:content_id,user_id,post_date
评论:comment_id,user_id,comment_date
如何获得按月和年分组的活跃唯一身份用户数? Active表示用户在该月份发布或评论。
答案 0 :(得分:4)
SELECT
T.action_year,
T.action_month,
COUNT(T.user_id) active_users
FROM
(
SELECT DISTINCT user_id, YEAR(post_date) action_year, MONTH(post_date) action_month FROM content
UNION
SELECT DISTINCT user_id, YEAR(comment_date) action_year, MONTH(comment_date) action_date FROM comment
) T
GROUP BY
T.action_year,
T.action_month
ORDER BY
T.action_year ASC,
T.action_month ASC
答案 1 :(得分:0)
这未经过测试。
select count(table1.user_id), table1.user_id
from table1 table2
where table1.user_id = table2.user_id
And table2.comment_date like 'may'
答案 2 :(得分:0)
试试这个
select TO_CHAR(order_dt,'MON-YYYY'), count(distinct User_ID ) cnt from [orders]
where User_ID in
(select User_ID from
(select a.User_ID from [orders] a,
(select a.User_ID,count (a.order_dt) from [orders] a
where a.order_dt > (select max(b.order_dt)-365 from [orders] b where a.User_ID=b.User_ID)
group by a.User_ID
having count(order_dt)>1) b
where a.User_ID=b.User_ID) a
)
group by TO_CHAR(order_dt,'MON-YYYY');