sql查询语句:求去年同期数据

如图:

图片说明

参考 https://blog.csdn.net/xuexiiphone/article/details/80514276 之前的问题采纳了,再详细写给你

with T_tmp as
(--把原始数据日期加一年,生产临时表(原来2020年的显示为21年,19年显示为20年)
select 国家,日期+1 as 日期,产量 from Table A )

select A.国家,A.日期,B.产量, A.产量 from Table A
right join T-tmp B
on A.国家 = B.国家
and A.日期 = B.日期

select a.*, b.产量 from t a
where 日期年份 = 2020
left join (
select * from t
where 日期年份 = 2019
) b
on a.country = b.country,
a.日期月 = b.日期月
order by a.count, a.date

如果不考虑国家,那一楼的思维是对的,如果考虑到国家同期数据,那就如下:

select a.*, b.产量 from t a
where 日期年份 = 2020
left join (
select * from t
where 日期年份 = 2019
) b
on a.country = b.country,
a.日期月 = b.日期月
group by a.国家