MS Access-为每次新购买添加最新价格



我在Purchases:表中处理这些数据

Mat_ID     Date          Price
11         5.1.2018      10
11         7.1.2018      12
11         9.1.2018      14
12         5.1.2018      10
12         7.1.2018      12
13         9.1.2018      14
13         5.1.2018      10

我想要的输出查询是有另一列最后购买价格:

Mat_ID     Date          Price   PrevPrice
11         5.1.2018      10      Null
11         7.1.2018      12      10
11         9.1.2018      14      12
12         5.1.2018      10      Null
12         7.1.2018      12      10
13         9.1.2018      14      Null
13         5.1.2018      10      14

你能推荐点什么吗?非常感谢。

试试这个:

Select 
*, 
(Select Top 1 Price 
From YourTable As T 
Where T.Mat_ID = YourTable.Mat_ID And T.[Date] < YourTable.[Date] 
Order By T.Mat_ID, T.[Date] Desc) As PrevPrice
From
YourTable

最新更新