SQL Server - 引导一组记录

时间:2018-06-14 08:22:01

标签: sql sql-server

我们说我有这样一张桌子:

 ID | version | feature | Startdate
  1 | 0.1     | A       | 01-01-2018
  2 | 0.1     | B       | 01-01-2018
  3 | 0.1     | C       | 01-01-2018
  4 | 0.2     | A       | 15-03-2018
  5 | 0.2     | B       | 15-03-2018
  6 | 0.2     | D       | 15-03-2018

startdate是功​​能实现的日期。如果某个功能出现2次(或更多次),则表示其版本已更新。在这种情况下,功能A的版本0.1在15-03-2018更新为0.2版。功能C已停止等。

我想结束每个功能及其版本的结束日期。所以在这种情况下,我想得到:

 ID | version | feature | Startdate  | Enddate
  1 | 0.1     | A       | 01-01-2018 | 15-03-2018
  2 | 0.1     | B       | 01-01-2018 | 15-03-2018
  3 | 0.1     | C       | 01-01-2018 | 15-03-2018
  4 | 0.2     | A       | 15-03-2018 | 31-12-9999
  5 | 0.2     | B       | 15-03-2018 | 31-12-9999
  6 | 0.2     | D       | 15-03-2018 | 31-12-9999

我用LEAD(StartDate,1,'9999-12-31') OVER (PARTITION BY Version)尝试过但是这只是表格中下一条记录的开始日期。

有任何建议/想法吗?

4 个答案:

答案 0 :(得分:1)

你太近了。

LEAD(StartDate,1,'9999-12-31') OVER (PARTITION BY Feature ORDER BY Version) 

您需要按feature进行分区,因为每个日期与下一个日期相关联,按版本排序。

答案 1 :(得分:1)

Message

输出

CREATE TABLE #Table1
    ([ID] int, [version] numeric(22,6), [feature] varchar(1), [Startdate] varchar(10))
;

INSERT INTO #Table1
    ([ID], [version], [feature], [Startdate])
VALUES
    (1, 0.1, 'A', '01-01-2018'),
    (2, 0.1, 'B', '01-01-2018'),
    (3, 0.1, 'C', '01-01-2018'),
    (4, 0.2, 'A', '15-03-2018'),
    (5, 0.2, 'B', '15-03-2018'),
    (6, 0.2, 'D', '15-03-2018')

WITH CTE
AS (
    SELECT *
        ,ROW_NUMBER() OVER (
            PARTITION BY VERSION ORDER BY ID
            ) AS RN
    FROM #TABLE1
    )
SELECT ID
    ,VERSION
    ,FEATURE
    ,STARTDATE
    ,LEAD(STARTDATE, 1, '9999-12-31') OVER (
        PARTITION BY RN ORDER BY RN
            ,ID
        ) ENDDATE
FROM CTE
ORDER BY ID

答案 2 :(得分:0)

请像这样使用 -

CREATE TABLE LeadTable
(
  ID INT 
 ,version FLOAT
 ,feature VARCHAR(1)
 ,Startdate DATE
)
GO

INSERT INTO LeadTable VALUES
(1,0.1,'A','2018-01-01'),
(2,0.1,'B','2018-01-01'),
(3,0.1,'C','2018-01-01'),
(4,0.2,'A','2018-03-15'),
(5,0.2,'B','2018-03-15'),
(6,0.2,'D','2018-03-15')
GO

SELECT a.* , ISNULL(x.EndDate,'9999-12-31') EndDate
FROM LeadTable a
OUTER APPLY
(   
    SELECT TOP 1 Startdate EndDate
    FROM LeadTable b
    WHERE a.version + 0.1 = b.version
)x

ID          version                feature Startdate  EndDate
----------- ---------------------- ------- ---------- ----------
1           0.1                    A       2018-01-01 2018-03-15
2           0.1                    B       2018-01-01 2018-03-15
3           0.1                    C       2018-01-01 2018-03-15
4           0.2                    A       2018-03-15 9999-12-31
5           0.2                    B       2018-03-15 9999-12-31
6           0.2                    D       2018-03-15 9999-12-31

(6 rows affected)

答案 3 :(得分:0)

请像这样使用 - 假设你的桌子是[产品]

    SELECT AA.ID, AA.version, AA.feature, AA.Startdate, 
    isnull(BB.Startdate,'31-12-9999') AS Enddate
    FROM (
        (
            SELECT ID, version, feature, Startdate, ROW_NUMBER() OVER (
                    PARTITION BY feature ORDER BY feature
                    ) AS Row_Number
            FROM product
            ORDER BY Row_Number, Startdate
            ) AA LEFT JOIN (
            SELECT ID, version, feature, Startdate, ROW_NUMBER() OVER (
                    PARTITION BY feature ORDER BY feature
                    ) AS Row_Number
            FROM product
            ORDER BY Row_Number, Startdate
            ) BB
            ON (AA.ID = (BB.ID - 1))
        ) CC
    ORDER BY AA.ID, AA.version, AA.feature