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

Popular posts from this blog

searchKeyword not working in AngularJS filter -

sequelize.js - Sequelize: sort by enum cases -

user interface - how to replace an ongoing process of image capture from another process call over the same ImageLabel in python's GUI TKinter -