|
发表于 2020-6-13 19:00:01
|
显示全部楼层
select A.产品名,MAX(A.时间) AS 时间,SUM(A.数量) AS 数量,A.单价 AS 单价1,B.单价 AS 单价2,A.单价-(select top 1 单价 from testtb where 产品名 = A.产品名 and 时间 < '2016-12-01' order by 时间 desc) AS 差价
FROM testtb A LEFT JOIN testtb B ON A.产品名=B.产品名
AND B.时间=(SELECT MAX(时间) FROM testtb WHERE 产品名=A.产品名 AND 时间<A.时间 AND (单价<>A.单价 OR 时间<'2016-12-01'))
WHERE A.时间>='2016-12-01' AND A.时间<='2016-12-31'
GROUP BY A.产品名,A.单价,B.单价
ORDER BY MAX(A.时间)
|
|