Is it possible to join these two tables to get the following result.
Table Stats
Date Cus_ID Pur Amount 2015-02-01 2585711 2 100 2015-02-02 2585711 5 250 2015-05-03 2585711 8 400 2015-02-01 2585475 2 100 2015-02-02 2585475 5 250 2015-05-03 2585475 8 400
Table Customer Reg_Date Cus_ID Gender Country 2014-04-11 2585711 Male Sweden 2015-02-01 2585475 Female Sweden
Expected Result Date Cus_ID Pur Amount Gender Country 2014-04-11 2585711 NULL NULL Male Sweden 2015-02-01 2585711 2 100 Male Sweden 2015-02-02 2585711 5 250 Male Sweden 2015-05-03 2585711 8 400 Male Sweden 2015-02-01 2585475 2 100 Female Sweden 2015-02-02 2585475 5 250 Female Sweden 2015-05-03 2585475 8 400 Female Sweden
if I use a left outer join to join the two tables, I get the following result Date Cus_ID Pur Amt Gender Country 2015-02-01 2585711 2 100 Male Sweden 2015-02-02 2585711 5 250 Male Sweden 2015-05-03 2585711 8 400 Male Sweden 2015-02-01 2585475 2 100 Female Sweden 2015-02-02 2585475 5 250 Female Sweden 2015-05-03 2585475 8 400 Female Sweden
My query: Select if(a.Date IS NULL,b.reg_date,a.date) as Date, b.cus_id, pur, Amount, gender, country from (Select * from stats) as a Left outer join (select * from customer) as b on a.cus_id = b.cus_id
Any help will be highly appreciated. Thanks!
I don't know if this is still relevant.
If the idea to show every date a customer was active, even if they didn't make a purchase, I'd start with a table that gives me all dates and customer pairs first.
select [date],[cust_id]
into #activity
from stats
union
select [reg_date],[cust_id]
from customer
select t1.*, t2.pur, t2.amt
from #activity t1
left outer join stats t2 on
t1.[date] = t2.[date] and t1.[cust_id] = t2.[cust_id]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.