1

我有一个表,其中包含链接到另一个表的优先级记录。

FK - Priority - PriorityUpdateDateTime

我要做的是向该表添加结束日期,但仅作为临时表。

所以我需要的是FK - Priority - StartDate - EndDate

其中 EndDate 是下一个 PriorityUpdateDateTime

IE

FK - Priority - PriorityUpdateDateTime

123 - 4 - 2011-02-25

123 - 2 - 2011-02-30

123 - 3 - 2011-03-10

变成

FK - Priority - StartDate - EndDate

123 - 4 - 2011-02-25 - 2011-02-30

123 - 2 - 2011-02-30 - 2011-03-10

123 - 3 - 2011-03-10 - NULL

谢谢你的帮助

4

2 回答 2

1

好的,您可以使用这样的子选择:

-- nasty sub select, lets see if we can do better
SELECT  T1.[FK], 
        T1.[Priority], 
        T1.[PriorityUpdateDateTime] AS 'StartDate', 
        (
            SELECT MAX(T2.[PriorityUpdateDateTime]) AS MP
            FROM @test AS T2
            WHERE T2.[FK] = T1.[FK]
                AND T2.[Priority] < T1.[Priority] 
        ) AS 'EndDate'
FROM @test AS T1

这是我创建测试表和数据的临时脚本

DECLARE @test AS TABLE
(
    [FK] INT NOT NULL,
    [Priority] INT NOT NULL,
    [PriorityUpdateDateTime] DATETIME NOT NULL
)

INSERT INTO @test VALUES(123, 4, '20110225')
INSERT INTO @test VALUES(123, 3, '20110228') -- there are only 28 days in Feb in 2011 (not 30)
INSERT INTO @test VALUES(123, 2, '20110310')

-- nasty sub select, lets see if we can do better
SELECT  T1.[FK], 
        T1.[Priority], 
        T1.[PriorityUpdateDateTime] AS 'StartDate', 
        (
            SELECT MAX(T2.[PriorityUpdateDateTime]) AS MP
            FROM @test AS T2
            WHERE T2.[FK] = T1.[FK]
                AND T2.[Priority] < T1.[Priority] 
        )
FROM @test AS T1
于 2012-06-25T01:40:33.043 回答
1

@马特@凯恩

凯恩做得很好,只是他选择了错误的不等式列。我只是做了一些更正。如果有人能想出一个更快的方法,而不是“古怪的更新”,我会感到惊讶,这可能会让相关的子查询和 CROSS APPLY 方法大开眼界。

这是构建更多测试数据的代码,而不是出于性能测试目的而动摇的数据。

--===== Conditionallly drop the test table to make reruns in SSMS easier
     IF OBJECT_ID('tempdb..#Test','U') IS NOT NULL
        DROP TABLE #Test
;
--===== Create and populate the test table on-the-fly
     -- using a "Pseudo Cursor" which is many times 
     -- faster than a WHILE loop.
 SELECT TOP 100000
        FK = ABS(CHECKSUM(NEWID()))%100+100, -- 100 thru 199
        Priority = ABS(CHECKSUM(NEWID()))%100+1, -- 1 thru 100
        PriorityUpdateDateTime = DATEADD(dd,
                                    ABS(CHECKSUM(NEWID()))%DATEDIFF(dd,'2000','2010')
                                 ,'2000') --20000101 thru 20091231
   INTO #Test
   FROM sys.all_columns ac1 --has more than 4000 rows even on a new system
  CROSS JOIN sys.all_columns ac2
;
--===== Create a clustered index to improve performance by about 10 times in this case
 CREATE INDEX IX_#Test ON #Test (FK,PriorityUpdateDateTime)
;

这是凯恩的代码的两种不同的演绎方式。详细信息在代码中。两者在大约相同的时间内返回相同的结果。

--===== Kane's correlated subquery works just fine here once we 
     -- flip it around and use a different column name in the 
     -- inequality part.
 SELECT t1.FK,  
        t1.Priority,  
        StartDate = t1.PriorityUpdateDateTime, 
        EndDate =  
            ( 
             SELECT MIN(t2.PriorityUpdateDateTime)
               FROM #Test t2 
              WHERE t2.FK = t1.FK 
                AND t2.PriorityUpdateDateTime > t1.PriorityUpdateDateTime  
            ) 
   FROM #Test t1 
  ORDER BY t1.FK, t1.PriorityUpdateDateTime, t1.Priority
;
--===== Or, you could use a CROSS APPLY and get the same thing because
     -- a CROSS APPLY isn't much more than a correlated sub-query.
 SELECT t1.FK,  
        t1.Priority,  
        StartDate = t1.PriorityUpdateDateTime,  
        d.EndDate
   FROM #Test t1 
  CROSS APPLY 
        ( 
         SELECT MIN(t2.PriorityUpdateDateTime)
           FROM #Test t2 
          WHERE t2.FK = t1.FK 
            AND t2.PriorityUpdateDateTime > t1.PriorityUpdateDateTime  
        ) d (EndDate)
  ORDER BY t1.FK, t1.PriorityUpdateDateTime, t1.Priority
;
于 2012-06-25T03:21:31.127 回答