SQL 服务器:从第 1 行减去第 2 行,从第 2 行减去第 3 行等
SQL Server :Substracting 2nd row from 1st ,3rd row from 2nd etc
我正在努力解决 SQL 服务器中的算法,我不知道如何解决这个问题。
我有一个包含时间戳的列,我已按 desc 对其进行了排序。
我需要找出第 2 行和第 1 行、第 3 行和第 2 行之间的区别,并将其显示在新行中。最终的 table 应该如下所示:
ID Type Time_Stamp Difference
xxx YYY 00:03:12 00:00:02
xxx ZZZ 00:03:14 00:00:02
xxx ZZZ 00:03:16
我可以使用某种 SQL 功能吗?如果您有任何想法,请告诉我。
干杯,
样本TABLE
CREATE TABLE #TEMP(ID VARCHAR(10),[TYPE] VARCHAR(10),TIME_STAMP TIME)
INSERT INTO #TEMP
SELECT 'xxx' , 'YYY', '00:03:12'
UNION ALL
SELECT 'xxx', 'ZZZ', '00:03:14'
UNION ALL
SELECT 'xxx', 'ZZZ', '00:03:16'
您需要执行自连接逻辑才能获取下一行的记录
查询
;WITH CTE AS
(
SELECT ROW_NUMBER()OVER(ORDER BY TIME_STAMP)RNO,*
FROM #TEMP
)
SELECT C1.*,'00:00:'+CAST(DATEDIFF(S,C1.TIME_STAMP,C2.TIME_STAMP)AS VARCHAR(2)) D
FROM CTE C1
LEFT JOIN CTE C2 ON C1.RNO=C2.RNO-1
- Click here 查看结果
使用Window Function
;WITH cte
AS (SELECT Row_number()
OVER(
ORDER BY time_stamp) rn,
*
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), Datediff(second, a.Time_Stamp, b.Time_Stamp)/3600)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (Datediff(second, a.Time_Stamp, b.Time_Stamp)%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), Datediff(second, a.Time_Stamp, b.Time_Stamp)%60), 2) AS [Difference]
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn - 1
如果您使用 Sql Server 2012+
则使用 Lead
函数
;WITH cte
AS (SELECT *,
Datediff(second, time_stamp, Lead(time_stamp)
OVER(
ORDER BY time_stamp)) AS Sec
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), sec/3600) + ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (sec%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), sec%60), 2) AS [Difference]
FROM cte a
更新: 要插入 temp table
,请执行此操作。
;WITH cte
AS (SELECT Row_number()
OVER(
ORDER BY time_stamp) rn,
*
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), Datediff(second, a.Time_Stamp, b.Time_Stamp)/3600)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (Datediff(second, a.Time_Stamp, b.Time_Stamp)%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), Datediff(second, a.Time_Stamp, b.Time_Stamp)%60), 2) AS [Difference]
into #tempTable --Here you need to use into temptable
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn - 1
或 create
温度 table 并使用
......
Insert into #temptable
SELECT a.ID,
a.Type,
a.Time_Stamp
......
我正在努力解决 SQL 服务器中的算法,我不知道如何解决这个问题。 我有一个包含时间戳的列,我已按 desc 对其进行了排序。
我需要找出第 2 行和第 1 行、第 3 行和第 2 行之间的区别,并将其显示在新行中。最终的 table 应该如下所示:
ID Type Time_Stamp Difference
xxx YYY 00:03:12 00:00:02
xxx ZZZ 00:03:14 00:00:02
xxx ZZZ 00:03:16
我可以使用某种 SQL 功能吗?如果您有任何想法,请告诉我。
干杯,
样本TABLE
CREATE TABLE #TEMP(ID VARCHAR(10),[TYPE] VARCHAR(10),TIME_STAMP TIME)
INSERT INTO #TEMP
SELECT 'xxx' , 'YYY', '00:03:12'
UNION ALL
SELECT 'xxx', 'ZZZ', '00:03:14'
UNION ALL
SELECT 'xxx', 'ZZZ', '00:03:16'
您需要执行自连接逻辑才能获取下一行的记录
查询
;WITH CTE AS
(
SELECT ROW_NUMBER()OVER(ORDER BY TIME_STAMP)RNO,*
FROM #TEMP
)
SELECT C1.*,'00:00:'+CAST(DATEDIFF(S,C1.TIME_STAMP,C2.TIME_STAMP)AS VARCHAR(2)) D
FROM CTE C1
LEFT JOIN CTE C2 ON C1.RNO=C2.RNO-1
- Click here 查看结果
使用Window Function
;WITH cte
AS (SELECT Row_number()
OVER(
ORDER BY time_stamp) rn,
*
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), Datediff(second, a.Time_Stamp, b.Time_Stamp)/3600)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (Datediff(second, a.Time_Stamp, b.Time_Stamp)%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), Datediff(second, a.Time_Stamp, b.Time_Stamp)%60), 2) AS [Difference]
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn - 1
如果您使用 Sql Server 2012+
则使用 Lead
函数
;WITH cte
AS (SELECT *,
Datediff(second, time_stamp, Lead(time_stamp)
OVER(
ORDER BY time_stamp)) AS Sec
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), sec/3600) + ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (sec%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), sec%60), 2) AS [Difference]
FROM cte a
更新: 要插入 temp table
,请执行此操作。
;WITH cte
AS (SELECT Row_number()
OVER(
ORDER BY time_stamp) rn,
*
FROM yourtable)
SELECT a.ID,
a.Type,
a.Time_Stamp,
CONVERT(VARCHAR(10), Datediff(second, a.Time_Stamp, b.Time_Stamp)/3600)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), (Datediff(second, a.Time_Stamp, b.Time_Stamp)%3600)/60), 2)
+ ':'
+ RIGHT('00'+CONVERT(VARCHAR(2), Datediff(second, a.Time_Stamp, b.Time_Stamp)%60), 2) AS [Difference]
into #tempTable --Here you need to use into temptable
FROM cte a
LEFT JOIN cte b
ON a.rn = b.rn - 1
或 create
温度 table 并使用
......
Insert into #temptable
SELECT a.ID,
a.Type,
a.Time_Stamp
......