sql - mysql join on date where one table's date is a month before -
i have 2 tables of want join on date. while date of same format, want join t1.date_1 t2.date_2 t2.date_2 month before. please see query below context , output want:
t1 date_1 count_t1 2015-01-01 10 2015-02-01 20 2015-03-01 30 t2 date_2 count_t2 2014-12-01 40 2015-01-01 50 2015-02-01 60 2015-03-01 70 output want: date_1 percent_before 2015-01-01 0.25 <--10/40 2015-02-01 0.40 <--20/50 2015-03-01 0.5 <--30/60 query: select (t1.count_1/t2.count_2) percent_before table1 t1 join table2 t2 on t1.date_1 = (t2.date_2 - 1 month);
you need interval keyword:
select t1.date_1, (t1.count_1 / t2.count_2) percent_before table1 t1 join table2 t2 on t1.date_1 = (t2.date_2 - interval 1 month);
Comments
Post a Comment