SQL:计算不同行中一列日期之间的天数

时间:2013-06-25 17:29:45

标签: sql sql-server-2008 date-arithmetic

根据我的数据,我有个人在不同的日期多次进行评估。它看起来像这样:

╔════════╦═══════════╦═══════════╦═══════╗
║ Person ║ ID Number ║ Date      ║ Score ║
║ John   ║ 134       ║ 7/11/2013 ║ 18    ║
║ John   ║ 134       ║ 8/23/2013 ║ 16    ║
║ John   ║ 134       ║ 9/30/2013 ║ 16    ║
║ Kate   ║ 887       ║ 2/28/2013 ║ 21    ║
║ Kate   ║ 887       ║ 3/16/2013 ║ 19    ║
║ Bill   ║ 990       ║ 4/18/2013 ║ 15    ║
║ Ken    ║ 265       ║ 2/12/2013 ║ 23    ║
║ Ken    ║ 265       ║ 4/25/2013 ║ 20    ║
║ Ken    ║ 265       ║ 6/20/2013 ║ 19    ║
║ Ken    ║ 265       ║ 7/15/2013 ║ 19    ║
╚════════╩═══════════╩═══════════╩═══════╝

我希望在最后有一个列,用于计算自该人第一次评估以来的天数。如果那更容易的话,我也会决定自上一次评估以来的天数。

理想情况下,它看起来像这样:

╔════════╦═══════════╦═══════════╦═══════╦══════════════════╗
║ Person ║ ID Number ║ Date      ║ Score ║ Days Since First ║
║ John   ║ 134       ║ 7/11/2013 ║ 18    ║ 0                ║
║ John   ║ 134       ║ 8/23/2013 ║ 16    ║ 43               ║
║ John   ║ 134       ║ 9/30/2013 ║ 16    ║ 81               ║
║ Kate   ║ 887       ║ 2/28/2013 ║ 21    ║ 0                ║
║ Kate   ║ 887       ║ 3/16/2013 ║ 19    ║ 16               ║
║ Bill   ║ 990       ║ 4/18/2013 ║ 15    ║ 0                ║
║ Ken    ║ 265       ║ 2/12/2013 ║ 23    ║ 0                ║
║ Ken    ║ 265       ║ 4/25/2013 ║ 20    ║ 72               ║
║ Ken    ║ 265       ║ 6/20/2013 ║ 19    ║ 128              ║
║ Ken    ║ 265       ║ 7/15/2013 ║ 19    ║ 153              ║
╚════════╩═══════════╩═══════════╩═══════╩══════════════════╝

我正在使用Microsoft SQL Server 2008

3 个答案:

答案 0 :(得分:5)

select  *
,       datediff(day, min(Date) over (partition by [ID Number]), Date)
from    YourTable

Live example at SQL Fiddle.

答案 1 :(得分:4)

我喜欢Andomar的答案,但如果您想找到自首次开始以来的两天和总天数,那么:

SELECT a.*
        ,ISNULL(DATEDIFF(day,b.Date,a.Date),0)'Since Previous'
        ,datediff(day, min(a.Date) over (partition by a.[ID Number]), a.Date)'Since First'
FROM (select  *,ROW_NUMBER() OVER(PARTITION BY [ID Number] ORDER BY DATE)RowRank
      from    YourTable
      )a
LEFT JOIN (select  *,ROW_NUMBER() OVER(PARTITION BY [ID Number] ORDER BY DATE)RowRank
      from    YourTable
      )b
ON a.[ID Number] = b.[ID Number]
 AND a.RowRank = b.RowRank + 1

演示:SQL Fiddle

答案 2 :(得分:1)

您可以使用APPLY运算符

选项

1.当前行日期与上一日期之间的差异

SELECT t1.*, 
       DATEDIFF(dd, ISNULL(o.[Date], t1.[Date]), t1.[Date]) AS [Days Since First]
FROM YourTable t1 OUTER APPLY (
                               SELECT TOP 1 [Date]
                               FROM YourTable t2
                               WHERE t1.[ID Number] = t2.[ID Number]
                                 AND t1.[Date] > t2.[Date]
                               ORDER BY t2.[Date] DESC
                               ) o

请参阅SQLFiddle

上的示例

2.自第一次评估以来的数天

SELECT t1.*, 
       DATEDIFF(dd, ISNULL(o.[Date], t1.[Date]), t1.[Date]) AS [Days Since First]
FROM YourTable t1 OUTER APPLY (
                               SELECT MIN(t2.[Date]) AS [Date]
                               FROM YourTable t2
                               WHERE t1.[ID Number] = t2.[ID Number]                                   
                               ) o

请参阅SQLFiddle

上的示例