我想生成一个跳过空值的序列号(即不计算空值):
PAT_ID ENC_DATE SEQ
------ -------- ---
1 null null
1 05/01/15 1
2 01/01/14 1
2 null null
2 02/11/14 2
目前:
SELECT PAT_ID, ENC_DATE
,case when enc_date is not null then ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY end_date) end SEQ
生成(空值增加SEQ值):
PAT_ID ENC_DATE SEQ
------ -------- ----
1 null null
1 05/01/15 2
2 01/01/14 1
2 null null
2 02/11/14 3
我错过了什么?
答案 0 :(得分:2)
您可以在NULL
:
PARTITION BY
项支票
SELECT PAT_ID, ENC_DATE
,CASE WHEN enc_date IS NOT NULL
THEN ROW_NUMBER() OVER (PARTITION BY pat_id,CASE WHEN Enc_Dt IS NULL THEN 1 END ORDER BY end_date)
END SEQ
,dense_rank() OVER (PARTITION BY pat_id ORDER BY enc_date) DR
答案 1 :(得分:1)
您希望最后考虑空日期:
SELECT PAT_ID, ENC_DATE
,case when enc_date is not null
then ROW_NUMBER() OVER (PARTITION BY pat_id ORDER BY ISNULL(enc_date, '2079-06-05T23:59:00')) end SEQ
,dense_rank() OVER (PARTITION BY pat_id ORDER BY ISNULL(enc_date, '2079-06-05T23:59:00')) DR
from t;
结果:
PAT_ID ENC_DATE SEQ DR
1 2015-05-01 1 1
1 (null) (null) 2
2 2014-01-01 1 1
2 2014-02-11 2 2
2 (null) (null) 3