比较每隔一行的时间差
Comparing time difference for every other row
我正在尝试确定对每隔一行使用 AR_Event_Creation_Date_Time 之间的时间长度(以天为单位)。例如,第 1 行和第 2 行、第 3 行和第 4 行、第 5 行和第 6 行之间的天数等。换句话说,每个偶数行都有一个天数值,每个奇数行都有一个 NULL。如果每个借款人编号只有两行,我下面的代码就可以工作,但当超过两行时就会下降。在结果中,注意 1002092539
的变化
SELECT Borrower_Number,
Workgroup_Name,
FORMAT(AR_Event_Creation_Date_Time,'d','en-us') AS Tag_Date,
Usr_Usrnm,
DATEDIFF(day, LAG(AR_Event_Creation_Date_Time,1) OVER(PARTITION BY
Borrower_Number Order By Borrower_Number), AR_Event_Creation_Date_Time) Diff
FROM Control_Mail
您需要添加行号。此外,您的 partition by
是不确定的:
SELECT Borrower_Number,
Workgroup_Name,
FORMAT(AR_Event_Creation_Date_Time,'d','en-us') AS Tag_Date,
Usr_Usrnm,
DATEDIFF(day, LAG(AR_Event_Creation_Date_Time,1) OVER(PARTITION BY Borrower_Number, (rn - 1) / 2 ORDER BY AR_Event_Creation_Date_Time),
AR_Event_Creation_Date_Time) Diff
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Borrower_Number ORDER BY AR_Event_Creation_Date_Time) AS rn
FROM Control_Mail
) C
```
我正在尝试确定对每隔一行使用 AR_Event_Creation_Date_Time 之间的时间长度(以天为单位)。例如,第 1 行和第 2 行、第 3 行和第 4 行、第 5 行和第 6 行之间的天数等。换句话说,每个偶数行都有一个天数值,每个奇数行都有一个 NULL。如果每个借款人编号只有两行,我下面的代码就可以工作,但当超过两行时就会下降。在结果中,注意 1002092539
的变化SELECT Borrower_Number,
Workgroup_Name,
FORMAT(AR_Event_Creation_Date_Time,'d','en-us') AS Tag_Date,
Usr_Usrnm,
DATEDIFF(day, LAG(AR_Event_Creation_Date_Time,1) OVER(PARTITION BY
Borrower_Number Order By Borrower_Number), AR_Event_Creation_Date_Time) Diff
FROM Control_Mail
您需要添加行号。此外,您的 partition by
是不确定的:
SELECT Borrower_Number,
Workgroup_Name,
FORMAT(AR_Event_Creation_Date_Time,'d','en-us') AS Tag_Date,
Usr_Usrnm,
DATEDIFF(day, LAG(AR_Event_Creation_Date_Time,1) OVER(PARTITION BY Borrower_Number, (rn - 1) / 2 ORDER BY AR_Event_Creation_Date_Time),
AR_Event_Creation_Date_Time) Diff
FROM (
SELECT *,
ROW_NUMBER() OVER (PARTITION BY Borrower_Number ORDER BY AR_Event_Creation_Date_Time) AS rn
FROM Control_Mail
) C
```