2013-10-21 56 views
2

我有一個數據結構是這樣的:如何確定連續的最新記錄在SQL Server

ID  Status  Date 
--- ------  ------------ 
1  I   2013-10-01 
1  A   2013-10-02 
1  A   2013-10-03 
1  I   2013-10-04 
1  I   2013-10-05 
1  I   2013-10-06 
1  A   2013-10-07 
1  I   2013-10-08 

我想確定I多少狀態是連續的。在這種情況下3(2013-10-04,05和06)。

是這樣的:

Status ID Total_consecutive 
------- --- ------------------ 
A  1 2 
I  1 3 

在此先感謝。

+0

您是否試圖確定連續日期的最大組或連續日期的所有組? –

+0

是你的日期保證是相隔一天嗎? – Laurence

+0

嗨,@JonLaMarr我想確定一個給定的ID是否有比給定的數字更多的連續數。 – Yatiac

回答

1

我相信這會給你正確的結果:

declare @t table 
(
    ID INT,  
    [Status] CHAR(1),  
    [Date] DATE 
); 
insert @t (ID, [Status], [Date]) 
values (1, 'I', '2013-10-01'), 
(1, 'A', '2013-10-02'), 
(1, 'A', '2013-10-03'), 
(1, 'I', '2013-10-04'), 
(1, 'I', '2013-10-05'), 
(1, 'I', '2013-10-05'), 
(1, 'I', '2013-10-05'), 
(1, 'I', '2013-10-06'), 
(1, 'A', '2013-10-10') 

;with a as 
(
select ID, [Status], [Date], 
row_number() over (partition by id order by [Status], [Date])- 
row_number() over (partition by id order by [Date], [Status]) rn1 

from @t 
) 
select ID, [Status], min([Date]) Start, max([Date]) [End], 
      count(*) [Total_consecutive] 
from a 
group by id, [Status], rn1 
having count(*) > 1 

結果:

ID Status Start  End   Total_consecutive 
1 A  2013-10-02 2013-10-03 2 
1 I  2013-10-04 2013-10-06 5 
2

此解決方案讓你也開始日期和結束日期:

DECLARE @MyTable TABLE 
(
    ID INT,  
    [Status] CHAR(1),  
    [Date] DATE 
); 
INSERT @MyTable (ID, [Status], [Date]) 
SELECT 1, 'I', '2013-10-01' UNION ALL 
SELECT 1, 'A', '2013-10-02' UNION ALL 
SELECT 1, 'A', '2013-10-03' UNION ALL 
SELECT 1, 'I', '2013-10-04' UNION ALL 
SELECT 1, 'I', '2013-10-05' UNION ALL 
SELECT 1, 'I', '2013-10-06' UNION ALL 
SELECT 1, 'A', '2013-10-07' UNION ALL 
SELECT 1, 'I', '2013-10-08'; 

SELECT y.ID, 
     y.[Status], 
     y.GroupID, 
     Start = MIN(y.[Date]), 
     [End] = MAX(y.[Date]), 
     [Days]= DATEDIFF(DAY, MIN(y.[Date]), MAX(y.[Date])) + 1 
FROM 
(
SELECT x.ID, 
     x.[Status], 
     x.[Date], 
     GroupID = DATEDIFF(DAY, 0, x.[Date]) - ROW_NUMBER() OVER(PARTITION BY x.[ID], x.[Status] ORDER BY x.[Date]) 
FROM @MyTable x 
) y 
GROUP BY y.ID, y.[Status], y.GroupID 
HAVING MIN(y.[Date]) <> MAX(y.[Date]); 

結果:

ID Status GroupID Start  End  Days 
-- ------ ------- ---------- ---------- ---- 
1 A  41546 2013-10-02 2013-10-03 2 
1 I  41547 2013-10-04 2013-10-06 3