2012-04-22 18 views
2

我有以下表格:mysql - 如何獲取每月發佈或評論的獨特活躍用戶數量?

內容:CONTENT_ID,USER_ID,POST_DATE

評論:COMMENT_ID,USER_ID,COM​​MENT_DATE

我怎樣才能通過分組活動的唯一用戶的計數月和年? 活躍將意味着用戶在該月份發佈或評論。

+0

1)如果在同一個月內用戶的信息和評論,它應該被計算兩次? 2)如果用戶在不同的月份發佈和評論,是否應該每月計算一次? – 2012-04-22 02:05:38

回答

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 
+0

哇,很棒的工作!謝謝 – MotoTribe 2012-04-22 02:02:28

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' 
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'); 
相關問題