使用RANK
和ROW_NUMBER
導出numberof分鐘添加到組中的每個日期(如果另一種方法我已經正確解釋你的問題)。
應該高達2012上工作(你不提的SQL Server您正在使用的版本)
CREATE TABLE #Seq(seq INT,date DATETIME2);
DECLARE @Now DATETIME2 = SYSDATETIME();
INSERT INTO #Seq
(seq, date)
VALUES
(0, DATEADD(MINUTE, 1, @Now))
,(0, DATEADD(MINUTE, 2, @Now))
,(0, DATEADD(MINUTE, 3, @Now))
,(1, DATEADD(MINUTE, 10, @Now))
,(1, DATEADD(MINUTE, 10, @Now))
,(1, DATEADD(MINUTE, 10, @Now))
,(2, DATEADD(MINUTE, 20, @Now))
,(2, DATEADD(MINUTE, 21, @Now))
,(2, DATEADD(MINUTE, 22, @Now));
SELECT * FROM #Seq;
WITH CTE AS(
SELECT
SEQ
,Date
,ROW_NUMBER() OVER (PARTITION BY seq ORDER BY Date) rn
,RANK() OVER (PARTITION BY seq ORDER BY Date) rnk
FROM #seq
)
UPDATE CTE SET Date = DATEADD(MINUTE, (rn-rnk), Date);
SELECT * FROM #Seq;
您使用MSSQL/ORACLE/MYSQL? – ymz
MSSQL - 我以爲我標記的SQL服務器 – alcor8