查找日期基于第一行的行之间的差异

时间:2015-11-10 05:45:22

标签: sql sql-server-2012 common-table-expression

我正在使用SQL Server 2012.我有一个QC检查日期,其中包含id,事件和事件日期。以下是供参考的数据。

 QUALITY_CHECK_NO   EVENT                   DATE
 101                QC EVENT 1              2015-01-07 00:00:00
 101                QC EVENT 2              2015-08-10 00:00:00
 101                QC EVENT 3              2015-08-13 00:00:00
 101                QC EVENT 4              2015-08-25 00:00:00
 101                QC EVENT 5              2015-08-11 00:00:00
 101                QC EVENT 6              2015-08-13 00:00:00
 101                QC EVENT 7              2015-08-18 00:00:00
 101                QC EVENT 8              2015-08-19 00:00:00
 101                QC EVENT 9              2015-08-24 00:00:00
 101                QC EVENT 10             2015-08-25 00:00:00
 101                QC EVENT 11             2015-08-25 00:00:00
 101                QC EVENT 12             2015-09-11 00:00:00
 101                QC EVENT 13             2015-09-25 00:00:00
 102                QC EVENT 1              2015-01-07 00:00:00
 102                QC EVENT 2              2015-08-10 00:00:00
 102                QC EVENT 3              2015-08-26 00:00:00
 102                QC EVENT 4              2015-08-27 00:00:00
 102                QC EVENT 5              2015-08-21 00:00:00
 102                QC EVENT 6              2015-08-20 00:00:00
 102                QC EVENT 7              2015-08-26 00:00:00
 102                QC EVENT 8              2015-08-26 00:00:00
 102                QC EVENT 9              2015-08-27 00:00:00
 102                QC EVENT 10             2015-08-27 00:00:00
 102                QC EVENT 11             2015-08-27 00:00:00
 102                QC EVENT 12             2015-09-11 00:00:00
 102                QC EVENT 13             2015-09-25 00:00:00
 103                QC EVENT 1              2015-01-07 00:00:00
 103                QC EVENT 2              2015-08-10 00:00:00
 103                QC EVENT 3              2015-08-12 00:00:00
 103                QC EVENT 4              2015-08-25 00:00:00
 103                QC EVENT 5              2015-08-21 00:00:00
 103                QC EVENT 6              2015-08-11 00:00:00
 103                QC EVENT 7              2015-08-20 00:00:00
 103                QC EVENT 8              2015-08-20 00:00:00
 103                QC EVENT 9              2015-08-24 00:00:00
 103                QC EVENT 10             2015-08-25 00:00:00
 103                QC EVENT 11             2015-08-25 00:00:00
 103                QC EVENT 12             2015-09-11 00:00:00
 103                QC EVENT 13             2015-09-25 00:00:00
 104                QC EVENT 1              2015-10-20 00:00:00
 104                QC EVENT 2              2015-10-21 00:00:00
 104                QC EVENT 3              2015-10-21 00:00:00
 104                QC EVENT 4              2015-10-23 00:00:00

我想找到每个QualityCheckNo的第一个事件日期和下一行之间的天数差异。我尝试使用CTE但是我得到的是每行之间的天数差异,而不是我要找的东西。

基本上我需要找出QC Event 1和EVENTS之间有多少天差异。像下面的东西..

 QUALITY_CHECK_NO   EVENT       DATE                  DaysDiff
 101                QC EVENT 1  2015-01-07 00:00:00    0
 101                QC EVENT 2  2015-08-10 00:00:00    215
 101                QC EVENT 3  2015-08-13 00:00:00    218
 101                QC EVENT 4  2015-08-25 00:00:00    230
 101                QC EVENT 5  2015-08-11 00:00:00    216
 101                QC EVENT 6  2015-08-13 00:00:00    218

下面是我使用的sql代码。

WITH tableNumbered AS (
        SELECT QUALITY_CHECK_NO, EVENT, DATE, ROW_NUMBER() OVER (PARTITION BY SiteID ORDER BY SITEID, RNUM) as rNum FROM [db_lexitas_ods].[dbo].[vw_GetPvtDocumentTAT] )
        SELECT t.*,
           DATEDIFF(day,t2.[DATE],t.[DATE])
      as Daysdiff
    FROM tableNumbered t
    LEFT JOIN tableNumbered t2 ON t.[QUALITY_CHECK_NO] = t2.[QUALITY_CHECK_NO] AND t2.rNum + 1 = t.rNum

4 个答案:

答案 0 :(得分:3)

select
    datediff(
        dd,
        min(EVENTDATE) over (partition by QUALITY_CHECK_NO),
        EVENTDATE
    )
from T

这假设事件1始终是最早的日期。 min表达式也可以由此替换,以按事件名称进行查找:

(
    select EVENTDATE from T t2
    where
            t2.QUALITY_CHECK_NO = t.QUALITY_CHECK_NO
        and EVENT = 'QC EVENT 1'
),

你的尝试几乎有效,除了连接条件需要在rNum上等于1而不是rNum + 1.并且因为你想要第一个差异为零,所以常规内连接也可以。

还有另一个已发布的答案,它使用group byjoin来完成同样的事情,查询计划可能与上面第二个选项中的相关子查询相同。使用标量子查询可能有一个优点,因为如果写错了并返回多行,它将抛出错误。

答案 1 :(得分:0)

下面的代码应该可以解决您的要求。

Select QUALITY_CHECK_NO, EVENT, DATE, datediff(day, (select top(1) DATE from <table_name>), DATE) as DaysDiff from <table_name>;

答案 2 :(得分:0)

SELECT 
    tableNumbered.Quality_Check_No
    ,tableNumbered.Event
    ,[tableNumbered.Date] [Date]
    ,DATEDIFF(d,Grp.MinDate,[tableNumbered.Date]) AS DateDiff
FROM 
     tableNumbered 
LEFT OUTER JOIN
    (SELECT Quality_Check_No, MIN(CONVERT(date,[Date]) AS MinDate)
     FROM tableNumbered 
     GROUP BY Quality_Check_No
    ) Grp ON tableNumbered.Quality_Check_No = Grp.Quality_Check_No

答案 3 :(得分:0)

如果您的唯一目的是找到上一行之间的天差,那么您可以使用LAG()。

SELECT QUALITY_CHECK_NO,EVENT,date,
DATEDIFF(day,LAG(DATE) OVER (ORDER BY EVENT),DATE)
FROM Table

可以找到LEAD()和LAG()的详细说明here

工作SQLFiddle相同。