2015-06-10 48 views
0

我有一個shopevent表,其中有列id,user_id,shop_date的交易行。 我可以拉店事件與sometthing像從購物表中獲取新的獨特購買用戶

SELECT * FROM SHOP_EVENTS A LEFT OUTER JOIN USER_TABLE B ON A.USER_ID = B.USER_ID WHERE B.USER_REGISTRATION_DT BETWEEN '2012-01-01' AND '2012-01-31' 

我也有一個用戶表從中我可以得到一個特定月份的註冊用戶。

SELECT * FROM USER_TABLE WHERE registration_date between '2012-01-01' AND '2012-01-31' 

我所想要實現許多新的獨特的用戶每個月都會進行購買,他們註冊之後。

這是我正在嘗試的查詢。

SELECT 
CASE 
WHEN shop_date BETWEEN  '2012-01-01' AND '2012-01-31' THEN 1 
WHEN shop_date BETWEEN  '2012-02-01' AND '2012-02-29' THEN 2 
WHEN shop_date BETWEEN  '2012-03-01' AND '2012-03-31' THEN 3 
WHEN shop_date BETWEEN  '2012-04-01' AND '2012-04-30' THEN 4 
WHEN shop_date BETWEEN  '2012-05-01' AND '2012-05-31' THEN 5 
WHEN shop_date BETWEEN  '2012-06-01' AND '2012-06-30 'THEN 6 
WHEN shop_date BETWEEN  '2012-07-01' AND '2012-07-31 'THEN 7 
WHEN shop_date BETWEEN  '2012-08-01' AND '2012-08-31 'THEN 8 
WHEN shop_date BETWEEN  '2012-09-01' AND '2012-09-30 'THEN 9 
WHEN shop_date BETWEEN  '2012-10-01' AND '2012-10-31 'THEN 10 
WHEN shop_date BETWEEN  '2012-11-01' AND '2012-11-30 'THEN 11 
WHEN shop_date BETWEEN  '2012-12-01' AND '2012-12-31 'THEN 12 
WHEN shop_date BETWEEN  '2013-01-01' AND '2013-01-31 'THEN 13 
WHEN shop_date BETWEEN  '2013-02-01' AND '2013-02-28 'THEN 14 
WHEN shop_date BETWEEN  '2013-03-01' AND '2013-03-31 'THEN 15 
WHEN shop_date BETWEEN  '2013-04-01' AND '2013-04-30 'THEN 16 
WHEN shop_date BETWEEN  '2013-05-01' AND '2013-05-31 'THEN 17 
WHEN shop_date BETWEEN  '2013-06-01' AND '2013-06-30 'THEN 18 
WHEN shop_date BETWEEN  '2013-07-01' AND '2013-07-31 'THEN 19 
WHEN shop_date BETWEEN  '2013-08-01' AND '2013-08-31 'THEN 20 
WHEN shop_date BETWEEN  '2013-09-01' AND '2013-09-30 'THEN 21 
WHEN shop_date BETWEEN  '2013-10-01' AND '2013-10-31 'THEN 22 
WHEN shop_date BETWEEN  '2013-11-01' AND '2013-11-30 'THEN 23 
WHEN shop_date BETWEEN  '2013-12-01' AND '2013-12-31 'THEN 24 
WHEN shop_date BETWEEN  '2014-01-01' AND '2014-01-31 'THEN 25 
WHEN shop_date BETWEEN  '2014-02-01' AND '2014-02-28 'THEN 26 
WHEN shop_date BETWEEN  '2014-03-01' AND '2014-03-31 'THEN 27 
WHEN shop_date BETWEEN  '2014-04-01' AND '2014-04-30 'THEN 28 
WHEN shop_date BETWEEN  '2014-05-01' AND '2014-05-31 'THEN 29 
WHEN shop_date BETWEEN  '2014-06-01' AND '2014-06-30 'THEN 30 
WHEN shop_date BETWEEN  '2014-07-01' AND '2014-07-31 'THEN 31 
WHEN shop_date BETWEEN  '2014-08-01' AND '2014-08-31' THEN 32 
WHEN shop_date BETWEEN  '2014-09-01' AND '2014-09-30' THEN 33 
WHEN shop_date BETWEEN  '2014-10-01' AND '2014-10-31' THEN 34 
WHEN shop_date BETWEEN  '2014-11-01' AND '2014-11-30' THEN 35 
WHEN shop_date BETWEEN  '2014-12-01' AND '2014-12-31' THEN 36 
WHEN shop_date BETWEEN  '2015-01-01' AND '2015-01-31' THEN 37 
WHEN shop_date BETWEEN  '2015-02-01' AND '2015-02-28' THEN 38 
WHEN shop_date BETWEEN  '2015-03-01' AND '2015-03-31' THEN 39 
WHEN shop_date BETWEEN  '2015-04-01' AND '2015-04-30' THEN 40 
WHEN shop_date BETWEEN '2015-05-01' AND '2015-05-31' THEN 41 
END AS month, 
count(user_id), 
count(DISTINCT user_id) 
FROM 
    shop_events a 
inner join user_table b on a.user_id = b.user_id 
WHERE 
    user_registration_dt between '2012-01-01' and '2012-01-31' 

這是給我每個月誰做購買起始月1.如何得到誰做購買新的獨特的用戶列表的唯一用戶。這意味着,可以說10,000個註冊表中有100個用戶在第1個月進行了購買。之後,有50個新用戶不是上個月的100個用戶進行購買。如何在像這樣的查詢中獲得新的獨特用戶?

任何幫助將是偉大的。

回答

0

你什麼真的想要的是有人第一次購買。對於那次購買,它們是獨一無二的。 (如果我正確理解你的問題。)你可以使用聚合來計算這個。

我不確定你想要什麼輸出。我也不明白「一個月」對你意味着什麼。但是,下列情況的計算按天:

select (shop_date - ut.user_registration_dt) as days_between, count(*) 
from User_Table ut JOIN 
    (select se.user_id, min(se.shop_date) as minsd 
     from shop_events se 
     group by se.user_id 
    ) se 
    on ut.user_id = se.user_id 
where ut.user_registration_dt between '2012-01-01' and '2012-01-31' 
group by days_between 
order by days_between; 
0

只是「一塊拼圖」:你的長篇CASE子句可以通過

MONTH(shop_date)+(YEAR(shop_date)-2012)*12 

而且更換也許你想這樣的事情:

-- lists for users of each registration month (uregmon) the shopping month no. (shopmon), 
-- the total count of shopping events (shopcount) and the number of different customers 
-- in that month (custcount). 
SELECT uregmon, shopmon, COUNT(suid) shopcount, COUNT(DISTINCT suid) custcount FROM 
(-- derived table: lists all user ids for each shop event in each numbered month 
SELECT MONTH(shop_date)+(YEAR(shop_date)-2012)*12 shopmon,USER_ID suid 
FROM SHOP_EVENTS 
) s JOIN 
(-- derived table: lists user ids and the month no. of their registration 
SELECT USER_ID uuid,MONTH(registration_date)+(YEAR(registration_date)-2012)*12 uregmon 
FROM USER_TABLE 
) u ON uuid=suid 
GROUP BY uregmon, shopmon 

如果過濾此表爲特定的登記一個月(即WHERE uregmon=1)西港島線你我的結果與原來的查詢相同。

相關問題