1
我運行一個查詢,並得到下面的結果重啓ROWNUMBER根據具體的情況
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
DATA
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
我怎樣才能進一步使分區並分配行號基於序列值, 例如我想重新啓動rownumber每次第一次在同一組HOSPcode,MRN
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
歡迎使用StackOverflow。請記住,SO讚賞是upvotes和接受的答案。我會強烈建議您仔細閱讀[常見問題解答]以獲取關於該網站的更多基本信息,尤其是[FAQ#HowToAsk]如何詢問 – 2012-03-14 05:45:00
當您通過[HOSP_CODE],[MRN]'分區時,所有行在同一個分區中將會在這些列中具有相同的值 - 那麼爲什麼你將它們包含在'ORDER BY'中呢? – 2012-03-14 08:00:50
所有這些「First」,「Middle」,「Final」值似乎都沒有太大意義,它可能足以只有一個'RestartsTheSequence位'列:'1' =序列重新啓動,'0 ' - 序列繼續。 – 2012-03-14 08:03:11