如何比较同一表中的2行(SQL Server)

时间:2019-04-29 20:46:48

标签: sql sql-server

Doc的数据如下:

id |  name | created_dt | version
-----------------------------------------
1   doc1    2018-12-26      1
2   doc2    2018-12-26      A
3   doc1    2019-01-26      2
4   doc1    2019-02-20      3
5   doc2    2019-02-16      B
6   doc3    2019-03-20      1
5   doc2    2019-04-26      C

任何实现此输出的好方法吗?

name | version |    days
---------------------------------
doc1       2            30
doc1       3            21
doc2       B            60
doc2       C            45

以下查询

select name, version, datediff(dd, a.created_dt, b.created_dt)
from doc a
inner join doc b on a.name = b.name and a.id > b.id

3 个答案:

答案 0 :(得分:1)

您似乎希望忽略当前版本和先前版本之间的日期,而忽略第一个版本。

您可以使用lag()来获取每个文档的先前版本的日期,并使用datediff()来计算天数差异。使用row_number(),您可以为每个文档编号,并过滤出第一个。

SELECT name,
       version,
       days
       FROM (SELECT name,
                    version,
                    datediff(day,
                             lag(created_dt) OVER (PARTITION BY name
                                                   ORDER BY version),
                             created_dt) days,
                    row_number() OVER (PARTITION BY name
                                       ORDER BY version) rn
                    FROM elbat) x
       WHERE rn <> 1
       ORDER BY name,
                version;

db<>fiddle

但是我看到你的电话不多了。我不知道,我可能弄错了,或者您在日期算术上出错了。

答案 1 :(得分:1)

您可以使用LAG()函数回顾按名称划分并按时间顺序排列的上一个created_dt。

DECLARE @doc TABLE(id INT, name NVARCHAR(50),created_dt DATETIME,version NVARCHAR(50))
INSERT @doc VALUES
(1,'doc1','12/26/2018','1'),
(2,'doc2','12/26/2018','A'),
(3,'doc1','01/26/2019','2'),
(4,'doc1','02/20/2019','3'),
(5,'doc2','02/16/2019','B'),
(6,'doc3','03/20/2019','1'),
(5,'doc2','04/26/2019','C')

SELECT
    name,
    version,
    days = DATEDIFF(DAY,PreviousDate,created_dt)
FROM
(
    SELECT
        name,
        version,
        created_dt,
        PreviousDate = LAG(created_dt) OVER (PARTITION BY name ORDER BY created_dt)
    FROM
        @doc
)AS X
WHERE
    NOT PreviousDate IS NULL

答案 2 :(得分:1)

假设您希望两个版本之间有几天的时间,这是您的SQL,其中一行与相同名称但先前版本比较:

DECLARE @MyTable TABLE (id INT,  name VARCHAR(10), created_dt DATE, version VARCHAR(10))
INSERT INTO @MyTable
(Id, name, created_dt, version)
VALUES
(1, 'doc1',  '2018-12-26', '1'),
(2, 'doc2',  '2018-12-26', 'A'),
(3, 'doc1',  '2019-01-26', '2'),
(4, 'doc1',  '2019-02-20', '3'),
(5, 'doc2',  '2019-02-16', 'B'),
(6, 'doc3',  '2019-03-20', '1'),
(5, 'doc2',  '2019-04-26', 'C')

SELECT * FROM @MyTable ORDER BY name

SELECT T1.name, T1.version, T1.created_dt CreatedT1, T2.created_dt CreatedT2, DATEDIFF(DAY, T2.created_dt, T1.created_dt) diff FROM @MyTable T1
    CROSS APPLY (SELECT TOP 1 * FROM @MyTable Tmp WHERE Tmp.name = T1.name AND Tmp.created_dt < T1.created_dt ORDER BY Tmp.created_dt) T2
    ORDER BY T1.id

结果(几天)我有所不同:

name    version CreatedT1   CreatedT2   diff
doc1    2   2019-01-26  2018-12-26  31
doc1    3   2019-02-20  2018-12-26  56
doc2    B   2019-02-16  2018-12-26  52
doc2    C   2019-04-26  2018-12-26  121