我有两张桌子,我想在约会时加入。虽然日期格式相同,但我希望t1.date_1
加入t2.date_2
,但t2.date_2
是一个月之前。请参阅下面的查询,了解上下文和我想要的输出:
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 i 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
my query:
select (t1.count_1/t2.count_2) as percent_before
from table1 t1 join table2 t2
on t1.date_1 = (t2.date_2 - 1 month);
答案 0 :(得分:0)
您需要interval
关键字:
select t1.date_1, (t1.count_1 / t2.count_2) as percent_before
from table1 t1 join
table2 t2
on t1.date_1 = (t2.date_2 - interval 1 month);
答案 1 :(得分:0)
对于使用teradata且无法使用间隔的人:
select t1.date_1, (t1.count_1 / t2.count_2) as percent_before
from table1 t1 join
table2 t2
on t1.date_1 = add_months(t2.date_2 - 1 month);