根据条件重新启动行号

时间:2012-03-14 05:28:57

标签: sql sql-server row-number

我正在运行一个查询并获得以下结果

Select *
from 
(Select ROW_NUMBER() over 
    (partition by [HOSP_CODE],[MRN] 
    order by [HOSP_CODE],MRN,ADM_DATETIME ) as rownumber,* 
from Live.tempnewbornbundling) as a
order by [HOSP_CODE],MRN,ADM_DATETIME

数据

Rownumber key     MRn    hospcode adm_date         sep_date         Sequence
--------- ------- ------ -------- ---------------- ---------------- --------
1         7099222 544607 3        22/07/2011 04:55 22/07/2011 10:44 First
2         7099223 544607 3        22/07/2011 10:45 25/07/2011 19:43 Middle
3         7099224 544607 3        25/07/2011 19:44 26/07/2011 11:29 Middle
4         7099225 544607 3        27/07/2011 12:30 27/07/2011 19:30 First
5         7099226 544607 3        27/07/2011 19:31 28/07/2011 19:31 Final
1         7099227 559282 3        03/07/2011 22:50 03/07/2011 23:51 First
2         7099228 559282 3        03/07/2011 23:52 04/07/2011 15:30 Middle
3         7099229 559282 3        04/07/2011 15:31 04/07/2011 17:59 Final
4         7099230 559282 3        05/07/2011 18:00 05/07/2011 18:05 First
5         7099231 559282 3        05/07/2011 18:06 09/07/2011 14:58 Final

如何根据序列值进一步制作分区并分配行号, 例如,我希望每次有相同的组HOSPcode,MRN

时重新启动rownumber
Rownumber key     MRn    hospcode adm_date         sep_date         Sequence New rownumber
--------- ------- ------ -------- ---------------- ---------------- -------- -------------
1         7099222 544607 3        22/07/2011 04:55 22/07/2011 10:44 First    1
2         7099223 544607 3        22/07/2011 10:45 25/07/2011 19:43 Middle   2
3         7099224 544607 3        25/07/2011 19:44 26/07/2011 11:29 Middle   3
4         7099225 544607 3        27/07/2011 12:30 27/07/2011 19:30 First    1
5         7099226 544607 3        27/07/2011 19:31 28/07/2011 19:31 Final    2
1         7099227 559282 3        03/07/2011 22:50 03/07/2011 23:51 First    1
2         7099228 559282 3        03/07/2011 23:52 04/07/2011 15:30 Middle   2
3         7099229 559282 3        04/07/2011 15:31 04/07/2011 17:59 Final    3
4         7099230 559282 3        05/07/2011 18:00 05/07/2011 18:05 First    1
5         7099231 559282 3        05/07/2011 18:06 09/07/2011 14:58 Final    2

1 个答案:

答案 0 :(得分:2)

您应该可以使用recursive CTE

执行此操作

你会想要在rownumber上进行递归(这将确保你结束),然后每次递增时继续增加一个新的rownumber列,当你先按下时重置。我相信下面的答案应该有效。我将以前的查询存储在临时表

WITH FinalValues (RowNumber, Key, MRN, HospCode, adm_date, sep_date, 
    sequence, NewRowNum)
AS
(
-- Anchor member definition
SELECT RowNumber, Key, MRN, HospCode, adm_date, sep_date, 
    sequence, 1 AS NewRowNum
FROM PreviousQuery
WHERE RowNumber = 1
UNION ALL
-- Recursive member definition
SELECT P.RowNumber, P.Key, P.MRN, P.HospCode, P.adm_date, P.sep_date, 
    P.sequence, 
    CASE WHEN P.sequence = 'First' THEN 1 ELSE NewRowNum + 1 END AS NewRowNum
FROM PreviousQuery AS P
INNER JOIN FinalValues 
    ON FinalValues.RowNumber = P.RowNumber + 1
)
-- Statement that executes the CTE
SELECT *
FROM FinalValues;
GO