答案 0 :(得分:0)
根据您的RDBMS及其版本,您可能可以使用窗口功能LAG,该功能可让您访问组中的上一行,例如:
SELECT
location,
product,
date,
price - LAG(price) OVER (PARTITION BY location, product ORDER BY date) change
FROM mytable
答案 1 :(得分:0)
或带有子查询:
SELECT price- (
SELECT TOP 1 price
FROM mytable m2
WHERE m2.location = m1.location and m2.product = m1.product and m2.date< m1.date
ORDER BY
date
)
FROM mytable m1