我有一個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個用戶進行購買。如何在像這樣的查詢中獲得新的獨特用戶?
任何幫助將是偉大的。