计算同一列中两个日期之间的日期差异

时间:2018-01-25 06:06:05

标签: sql sql-server

嗨,我有一个输入表格式,如。

ID1    ID2       date
1002    9648    2011-01-02
1003    9648    2011-06-06
1004    9648    2012-08-08
1005    9648    2016-01-06
1006    9648    2016-09-12
1007    9648    2018-01-22
1009    9744    2009-10-03
1010    9744    2012-01-10
1011    9744    2016-09-23
1012    9744    2017-10-25
1013    9923    2006-10-10
1014    10124   2017-10-11
1015    10124   2018-01-24

正在寻找输出表,如下所示。 你能帮我解决一下sql查询或者如何在talend中实现它。

如果两个日期之间的天数没有超过1096,大约3年我想把它作为零,并将类型更新为新的。

ID1      ID2    date    daysdifference  type
1002    9648    2011-01-02  0           new
1003    9648    2011-06-06  156         old
1004    9648    2012-08-08  429         old
1005    9648    2016-01-06  0           new
1006    9648    2016-09-12  250         old
1007    9648    2018-01-22  497         old
1009    9744    2009-10-03  0           new
1010    9744    2012-01-10  829         old
1011    9744    2016-09-23  0           new
1012    9744    2017-10-25  397         old
1013    9923    2006-10-10  0           new
1014    10124   2017-10-11  0           new
1015    10124   2018-01-24  91          old

谢谢,

Ankush Reddy。

2 个答案:

答案 0 :(得分:2)

您可以使用LAG功能获得所需的效果。基本上,LAG函数访问前一行的数据。从那里,您现在可以使用DATEDIFF函数计算上一个日期和当前日期之间的差异。但是,此功能从2012年开始存在。

SELECT  ID1, ID2, Date, 
        DaysDifference = CASE WHEN PreviousDate IS NULL THEN 0 ELSE 
                CASE WHEN Date_Diff < 1096 THEN Date_Diff ELSE 0 END END,
        CASE WHEN PreviousDate IS NULL THEN 'NEW' ELSE 
                CASE WHEN Date_Diff < 1096 THEN 'OLD' ELSE 'NEW' END END
FROM 
        (
            SELECT  *,
                    LAG(date, 1,NULL) OVER (PARTITION BY ID2 ORDER BY ID1) AS PreviousDate ,
                    DATEDIFF(DAY, LAG(date, 1,0) OVER (PARTITION BY ID2 ORDER BY ID1), date) Date_Diff
            FROM    TableA
        ) a

这是Demo

但是,如果您使用的是2012年以下的版本,您仍然可以使用ROW_NUMBER()

获得所需的结果
;WITH rows  AS
(
    SELECT  *,
            RN = ROW_NUMBER() OVER (PARTITION BY ID2 ORDER BY ID1)
    FROM    TableA
)
SELECT  ID1, ID2, Date, 
        DaysDifference = CASE WHEN PreviousDate IS NULL THEN 0 ELSE 
                CASE WHEN Date_Diff < 1096 THEN Date_Diff ELSE 0 END END,
        CASE WHEN PreviousDate IS NULL THEN 'NEW' ELSE 
                CASE WHEN Date_Diff < 1096 THEN 'OLD' ELSE 'NEW' END END

FROM
        (
            SELECT  a.ID1, a.ID2, a.Date, b.Date as PreviousDate,
                    DATEDIFF(DAY, b.date, a.date) Date_Diff
            FROM    rows a
                    LEFT JOIN rows b
                        ON a.RN = b.RN + 1
                            AND a.ID2 = b.ID2
        ) a
ORDER BY ID1, ID2

这是Demo

答案 1 :(得分:0)

试试这个:

select [id1], [id2], [date],
   case when [daysdifference] > 1096 then 0 else [daysdifference] end [daysdifference],
   case when [daysdifference] > 1096 then 'new' else 'old' end [type]
from (
   select *, abs(isnull(datediff(day, LAG([date], 1) over (order by id2, id1), 
[date]), 0)) [daysdifference] from @x
) [a]