分组连续记录

时间:2018-12-07 22:27:23

标签: sql sql-server tsql

我正在尝试对一些记录进行分组,以找到给定客户特定站点的第一个记录。问题是记录在站点之间来回移动,因此我需要将非连续站点日期范围分开。

鉴于示例数据,我想得出3条记录-一个用于站点1的记录始于7/3/18,第二个用于站点2的记录始于9/3/18,第三个用于站点1的记录再次始于11 / 3/18。

SELECT 9999 AS CLIENT_ID, 1 AS SITE_NUM, '2018-07-03' AS START_DATE, '2018-08-05' AS CREATED_DATE, 1 AS RECORD_ID
INTO #TEMP
UNION 
SELECT 9999 AS MEMBER_ID, 1 AS SITE_NUM, '2018-08-01' AS CONSENT_SIGN_DATE, '2018-10-05' AS CREATED_DATE, 2
UNION 
SELECT 9999 AS MEMBER_ID, 1 AS SITE_NUM, '2018-07-03' AS CONSENT_SIGN_DATE, '2018-09-22' AS CREATED_DATE, 3
UNION 
SELECT 9999 AS MEMBER_ID, 2 AS SITE_NUM, '2018-09-03' AS CONSENT_SIGN_DATE, '2018-09-05' AS CREATED_DATE, 4
UNION 
SELECT 9999 AS MEMBER_ID, 2 AS SITE_NUM, '2018-10-03' AS CONSENT_SIGN_DATE, '2018-10-05' AS CREATED_DATE, 5
UNION 
SELECT 9999 AS MEMBER_ID, 1 AS SITE_NUM, '2018-11-03' AS CONSENT_SIGN_DATE, '2018-11-05' AS CREATED_DATE, 6
UNION 
SELECT 9999 AS MEMBER_ID, 1 AS SITE_NUM, '2018-12-01' AS CONSENT_SIGN_DATE, '2018-12-05' AS CREATED_DATE, 7

我一直在玩ROW_NUM,但无法弄清楚如何将网站1的两组日期分开。

SELECT *, ROW_NUMBER()OVER(PARTITION BY T.CLIENT_ID, T.SITE_NUM ORDER BY T.START_DATE, T.RECORD_ID)
FROM #TEMP T 
LEFT JOIN #TEMP T2 ON T2.CLIENT_ID = T.CLIENT_ID AND T2.RECORD_ID = T.RECORD_ID - 1
ORDER BY T.RECORD_ID

如何为单个站点按客户和连续日期对结果进行分组?

2 个答案:

答案 0 :(得分:1)

您想要的连续行不应具有相同的SITE_NUM值。您需要做的就是在查询末尾添加一个where子句。

T2.SITE_NUM IS NULL

编辑,如@SteveB所建议的那样,添加{{1}}也可以显示最后一条记录。

答案 1 :(得分:1)

这是一个孤岛问题。为此,行号的不同是最好的方法:

select t.client_id, t.site_num, min(t.start_date), max(t.start_date)
from (select t.*,
             row_number() over (partition by t.client_id order by T.START_DATE, T.RECORD_ID) as seqnum_c,
             row_number() over (partition by t.client_id, t.site_num order by T.START_DATE, T.RECORD_ID) as seqnum_cs
      from #temp t
     ) t
group by client_id, site_num, (seqnum_c - seqnum_cs)