https://blog.csdn.net/weixin_40439880/article/details/78677689这个里面有可以参考的
SELECT T.OrderDate,T.数量,s.数量,a.数量
from
(
SELECT OrderDate,SUM(数量) AS 数量 FROM OrderTable
)t
left join
(
SELECT OrderDate,SUM(数量) as 数量 FROM OrderTable
)s on YEAR(t.OrderDate)=YEAR(DATEADD(YEAR,-1,s.OrderDate)) AND MONTH(t.OrderDate)=MONTH(s.OrderDate)AND DAY(t.OrderDate)=DAY(s.OrderDate) -- 同期
left join
(
SELECT OrderDate,SUM(数量) as 数量 FROM OrderTable
)a
on YEAR(t.OrderDate)=YEAR(a.OrderDate)
AND MONTH(t.OrderDate)=MONTH(DATEADD(MONTH,-1,a.OrderDate))AND DAY(t.OrderDate)=DAY(a.OrderDate) -- 上期