得到一个查询,该查询获取按日期,运营商,货源,路线分组的预订数据,我想自行加入该数据,以便我拥有与之相同的数据,但在上一年的日期加入(添加1天以对齐工作日) )。
例如,如果我们有
date |num|
----------|---|
2017-01-01|300|
2017-01-02|301|
2017-01-03|302|
2017-01-04|303|
2017-01-05|304|
2017-01-06|305|
2017-01-07|306|
2017-01-08|307|
2017-01-09|308|
2017-01-10|309|
...
2018-01-01|501|
2018-01-02|502|
2018-01-03|503|
2018-01-04|504|
2018-01-05|505|
2018-01-06|506|
2018-01-07|507|
2018-01-08|508|
2018-01-09|509|
2018-01-10|510|
...
然后我希望结果是
date |num| date |num|
----------|---| ----------|---|
2017-01-01|300| null |null|
2017-01-02|301| null |null|
2017-01-03|302| null |null|
2017-01-04|303| null |null|
2017-01-05|304| null |null|
2017-01-06|305| null |null|
2017-01-07|306| null |null|
2017-01-08|307| null |null|
2017-01-09|308| null |null|
2017-01-10|309| null |null|
...
2018-01-01|501| 2017-01-02|301|
2018-01-02|502| 2017-01-03|302|
2018-01-03|503| 2017-01-04|303|
2018-01-04|504| 2017-01-05|304|
2018-01-05|505| 2017-01-06|305|
2018-01-06|506| 2017-01-07|306|
2018-01-07|507| 2017-01-08|307|
2018-01-08|508| 2017-01-09|308|
2018-01-09|509| 2017-01-10|309|
2018-01-10|510| 2017-01-11|...|
我到目前为止所拥有的
select
a.date as date1
,a.source as source1
,a.operator as operator1
,a.route as route1
,a.sum as bookings1
,b.date as date2
,b.source as source2
,b.operator as operator2
,b.route as route2
,b.sum as bookings2
from
(select
date
,source
,operator
,route
,sum(record_count)
from booking
where transaction_date >= dateadd(year, -2, current_date)
group by 1,2,3,4) a
full outer join
(select
date
,source
,operator
,route
,sum(record_count)
from booking
where date between dateadd(year, -3, current_date) and dateadd(year, -1, current_date)
group by 1,2,3,4) b
on
b.date = dateadd(day, 1, dateadd(year, -1, a.date))
and a.source = b.source
and a.outbound_operator_name = b.outbound_operator_name
and a.outbound_route_name = b.outbound_route_name
;
上述查询的问题在于它显示的是正确的2018年结果,而不是正确的2017年结果。我认为连接类型或on子句中有问题,例如日期计算尝试...
任何帮助将不胜感激