比较上一个和当前日期的值

bkhjykvo  于 2022-10-03  发布在  其他
关注(0)|答案(2)|浏览(200)

我有一个关于SQL Server的问题:如何获取前一天的值?

CREATE TABLE [dbo].[productdetails]
(
    [productname] [varchar](50) NULL,
    [productdate] [date] NULL,
    [price] [int] NULL
) 

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-22' AS Date), 10)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-23' AS Date), 20)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-25' AS Date), 30)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-26' AS Date), 40)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-20' AS Date), 30)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-22' AS Date), 40)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-23' AS Date), 50)

INSERT INTO [dbo].[productdetails] ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-26' AS Date), 60)

基于上述数据,我希望输出如下:

ProductName|ProductDate|价格|previousday价格
-|-|
Lux|2022-02-22|10|空
勒克斯|2022-02-23|20|10
Lux|2022-02-25|30|空
勒克斯|2022-02-26|40|30
PEN|2022-01-20|30|空
PEN|2022-01-22|40|空
笔|2022-01-23|50|40
PEN|2022-01-26|60|空

在这里,当我们将该产品名称lux和ProductDate=2022-02-25与之前日期2022-02-24进行比较时,我们没有该产品的任何记录。

因此,我们需要在前一天的价格字段中显示值为空。

select  
    *,
    lag(price) over (partition by productname order by productdate ) as previousdayprice
from 
    [test].[dbo].[productdetails]
order by 
    productname, productdate

此查询不返回异常结果。您能告诉我如何在SQL Server中编写查询来实现这一任务吗?

r6vfmomb

r6vfmomb1#

你可以用下面的查询达到预期的效果。

Select p2.productname, p2.productdate, p2.price, p1.price AS previousdayprice from dbo.productdetails p1 
right outer join dbo.productdetails p2
on p1.productname = p2.productname and p1.productdate = DateAdd(day,-1,p2.productdate)
55ooxyrt

55ooxyrt2#

在下面,使用外部应用对所有日期的后代进行排序。

然后,我们按每个产品的最后产品日期进行筛选,每个产品的日期必须大于之前的产品日期。因此,我们得到了所需的结果集。

使用表变量是为了添加它。

DECLARE  @productdetails TABLE
(
    [productname] [varchar](50) NULL,
    [productdate] [date] NULL,
    [price] [int] NULL
) 

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-22' AS Date), 10)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-23' AS Date), 20)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-25' AS Date), 30)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'lux', CAST(N'2022-02-26' AS Date), 40)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-20' AS Date), 30)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-22' AS Date), 40)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-23' AS Date), 50)

INSERT INTO @productdetails ([productname], [productdate], [price]) 
VALUES (N'pen', CAST(N'2022-01-26' AS Date), 60)

结果查询;

SELECT  p1.[productname]
        ,p1.productdate
        ,p1.price
        ,p2.previousprice 
FROM 
    @productdetails as p1
    OUTER APPLY(SELECT TOP 1 previousprice=p2.price
            FROM 
                @productdetails as p2 
            WHERE 
                p1.productname=p2.productname 
                AND p1.productdate>p2.productdate 
            ORDER BY p2.productdate DESC 
            ) as p2
ORDER BY productname,productdate

结果集;

<table border = 1>   <tr>  <th> [productname] </th> <th>[productdate]</th> <th>[price]</th> <th>[previousprice]</th></tr><tr><td>lux</td><td>2022-02-22</td><td>10</td></tr><tr><td>lux</td><td>2022-02-23</td><td>20</td><td>10</td></tr><tr><td>lux</td><td>2022-02-25</td><td>30</td><td>20</td></tr><tr><td>lux</td><td>2022-02-26</td><td>40</td><td>30</td></tr><tr><td>pen</td><td>2022-01-20</td><td>30</td></tr><tr><td>pen</td><td>2022-01-22</td><td>40</td><td>30</td></tr><tr><td>pen</td><td>2022-01-23</td><td>50</td><td>40</td></tr><tr><td>pen</td><td>2022-01-26</td><td>60</td><td>50</td></tr></table>

相关问题