我花了很多時間在這里和谷歌上尋找解決方案,但一無所獲。我認為dense_rank() 可能有效,但我無法讓它做我需要的事情。我正在使用 SSMS 18。我正在嘗試為使用 row_number() 磁區的記錄組分配唯一 ID。
資料如下所示:
| 注冊護士 | Client_ID | 日期 |
|---|---|---|
| 1 | xxxx | 2022-08-23 |
| 2 | xxxx | 2022-08-23 |
| 3 | xxxx | 2022-08-23 |
| 1 | xxxx | 2022-08-25 |
| 2 | xxxx | 2022-08-25 |
| 1 | 年年 | 2022-06-10 |
| 2 | 年年 | 2022-06-10 |
| 1 | 呸呸呸 | 2021-05-06 |
| 2 | 呸呸呸 | 2021-05-06 |
| 3 | 呸呸呸 | 2021-05-06 |
| 4 | 呸呸呸 | 2021-05-06 |
所以現在每組記錄都需要有一個唯一的 ID。所以它看起來像這樣:
| 非qID | 注冊護士 | Client_ID | 日期 |
|---|---|---|---|
| 0001 | 1 | xxxx | 2022-08-23 |
| 0001 | 2 | xxxx | 2022-08-23 |
| 0001 | 3 | xxxx | 2022-08-23 |
| 0002 | 1 | xxxx | 2022-08-25 |
| 0002 | 2 | xxxx | 2022-08-25 |
| 0003 | 1 | 年年 | 2022-06-10 |
| 0003 | 2 | 年年 | 2022-06-10 |
| 0004 | 1 | 呸呸呸 | 2021-05-06 |
| 0004 | 2 | 呸呸呸 | 2021-05-06 |
| 0004 | 3 | 呸呸呸 | 2021-05-06 |
| 0004 | 4 | 呸呸呸 | 2021-05-06 |
提前感謝您對此的任何幫助。
row_number()
over (partition by a.PAT_ID, cast(a.EFFECTIVE_DATE_DT as date)
order by case
when a.[EncType_C] = '1000' then 1
when a.[EncType_C] = '101' then 2
when a.[EncType_C] = '3'
and (dpt.DeptNm not like '%X-RAY%' and dpt.DeptNm != 'VCM LAB') then 3
when a.[EncType_C] = '50' then 4
when a.[EncType_C] = '3'
and (dpt.DeptNm like '%X-RAY%' or dpt.DeptNm = 'VCM LAB') then 5
else 6
end,
case
when a.[DeptID] = 100101024 then 1
when a.[DeptID] = 100101055 then 2
else 0
end)
uj5u.com熱心網友回復:
嘗試使用通用表條目 cte 和 groupby
declare @tmp as table(RN int, Client_ID varchar(10), Date Date);
insert into @tmp
values
(1,'xxxx','2022-08-23'),
(2,'xxxx','2022-08-23'),
(3,'xxxx','2022-08-23'),
(1,'xxxx','2022-08-25'),
(2,'xxxx','2022-08-25'),
(1,'yyyy','2022-06-10'),
(2,'yyyy','2022-06-10'),
(1,'gggg','2021-05-06'),
(2,'gggg','2021-05-06'),
(3,'gggg','2021-05-06'),
(4,'gggg','2021-05-06');
with cte
as
(
select
distinct
Client_ID,
Date,
Count(Client_ID) over(order by Client_ID,Date) RowID
from @tmp
group by
Client_ID,
Date
)
--select * from cte
select Client_ID, Date,
Lookup.UnqID
from @tmp tmp
cross apply
(
select RowID UnqID from cte
where tmp.Client_ID=cte.Client_ID and tmp.Date=cte.Date
)Lookup
輸出
Client_ID Date UnqID
gggg 2021-05-06 1
gggg 2021-05-06 1
gggg 2021-05-06 1
gggg 2021-05-06 1
xxxx 2022-08-23 2
xxxx 2022-08-23 2
xxxx 2022-08-23 2
xxxx 2022-08-25 3
xxxx 2022-08-25 3
yyyy 2022-06-10 4
yyyy 2022-06-10 4
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/441838.html
標籤:sql
上一篇:需要幫助從DB2獲取資料
