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!
if(a.Date IS NULL,b.reg_date,a.date) as Datesince you use aLEFT OUTER JOINa.date is not going to be NULL (unless it's NULL in thestatstable). In yourExepected Resultswhere did that first record come from? Why is that record significant? - JNevill