双倍增量,其中第二个增量反映了 sql 中遇到数据的第一个
Double increment where 2nd increment reflects 1st in sql for encounter data
我正在构建 healthcare 837 encounters,需要在 HL 段上设置增量。
C1
基于 Criteria1
的设置,C2
基于 Criteria2
.
C2
永远不会与 C1
有相同的数字,反之亦然。
C1
我可以使用 row_number() over(order by (select Criteria1))
这是 C2
我遇到的问题。
C1 | C2 | Criteria1 | Criteria2
1 | 2 | ID1 | NID1
1 | 3 | ID1 | NID2
1 | 4 | ID1 | NID3
5 | 6 | ID2 | NID4
5 | 7 | ID2 | NID5
5 | 8 | ID2 | NID6
9 |10 | ID3 | NID7
简化查询:
SELECT cm.Criteria1, cm.Criteria2, cj.C1
FROM [dbo].[TBL1] cm
JOIN (
SELECT cm.Criteria1,
row_number() over(order by (select Criteria1)) as C1
FROM [dbo].[TBL1] cm
GROUP BY cm.Criteria1) cj on cj.Criteria1 = cm.Criteria1
GROUP BY cm.Criteria1, cm.Criteria2, cj.C1 Order by cj.C1
不太清楚你到底想得到什么,但看起来下面就是你要找的东西:
with counts as (select count(distinct cm.criteria1) c1, count(distinct cm.criteria2) c2 from dbo.tbl1)
select cj1.c1, cj2.c2, cm.criteria1, cm.criteria2
from dbo.tbl1 cm
inner join (
select cm1.criteria1,
row_number() over ( order by cm1.criteria1)as c1
from dbo.tbl1 cm1 group by cm1.criteria1) cj1
on cj1.criteria1 = cm.criteria1
inner join (
select cm2.criteria2,
(select counts.c1 from counts) + row_number() over ( order by cm2.criteria2) as c2
from dbo.tbl1 cm2 group by cm1.criteria2) cj2
on cj2.criteria2 = cm.criteria2
group by cm.criteria1, cm.criteria2, cj1.c1, cj2.c2
order by cj1.c1, cj2.c2
这似乎可行,但我没有检查很多边缘情况(开窗很有趣!):
with tbl1 as (
select 'ID1' as Criteria1, 'NID1' as Criteria2
union
select 'ID1', 'NID2'
union
select 'ID2', 'NID4'
union
select 'ID2', 'NID5'
union
select 'ID3', 'NID7'
)
select
rank() over (order by Criteria1) + DENSE_ranK() OVER (ORDER BY CRITERIA1) - 1 as C1,
rank() over (order by Criteria1) + row_number() over (partition by Criteria1 order by Criteria2) + DENSE_ranK() OVER (ORDER BY CRITERIA1) - 1 as C2,
Criteria1,
Criteria2
from
tbl1
稍微分解一下:
按照SQL 的说法,我们将每组 Criteria1 行称为 "partition"。
因此要求是:
C1 始终等于所有先前分区中的行数 + 当前分区的 1,加上先前分区的数量。
C2总是等于之前所有分区的行数+当前分区的1,加上之前的分区数,加上分区内所有之前的行数+当前行的1 .
RANK() over (order by Criteria1)
为您提供所有先前分区中的行数 + 1。
DENSE_RANK() over (order by Criteria1) - 1
给出之前分区的数量。
ROW_NUMBER() over (partition by Criteria1 order by Criteria2)
为您提供分区中之前的行数。
我正在构建 healthcare 837 encounters,需要在 HL 段上设置增量。
C1
基于Criteria1
的设置,C2
基于Criteria2
.C2
永远不会与C1
有相同的数字,反之亦然。C1
我可以使用row_number() over(order by (select Criteria1))
这是 C2
我遇到的问题。
C1 | C2 | Criteria1 | Criteria2
1 | 2 | ID1 | NID1
1 | 3 | ID1 | NID2
1 | 4 | ID1 | NID3
5 | 6 | ID2 | NID4
5 | 7 | ID2 | NID5
5 | 8 | ID2 | NID6
9 |10 | ID3 | NID7
简化查询:
SELECT cm.Criteria1, cm.Criteria2, cj.C1
FROM [dbo].[TBL1] cm
JOIN (
SELECT cm.Criteria1,
row_number() over(order by (select Criteria1)) as C1
FROM [dbo].[TBL1] cm
GROUP BY cm.Criteria1) cj on cj.Criteria1 = cm.Criteria1
GROUP BY cm.Criteria1, cm.Criteria2, cj.C1 Order by cj.C1
不太清楚你到底想得到什么,但看起来下面就是你要找的东西:
with counts as (select count(distinct cm.criteria1) c1, count(distinct cm.criteria2) c2 from dbo.tbl1)
select cj1.c1, cj2.c2, cm.criteria1, cm.criteria2
from dbo.tbl1 cm
inner join (
select cm1.criteria1,
row_number() over ( order by cm1.criteria1)as c1
from dbo.tbl1 cm1 group by cm1.criteria1) cj1
on cj1.criteria1 = cm.criteria1
inner join (
select cm2.criteria2,
(select counts.c1 from counts) + row_number() over ( order by cm2.criteria2) as c2
from dbo.tbl1 cm2 group by cm1.criteria2) cj2
on cj2.criteria2 = cm.criteria2
group by cm.criteria1, cm.criteria2, cj1.c1, cj2.c2
order by cj1.c1, cj2.c2
这似乎可行,但我没有检查很多边缘情况(开窗很有趣!):
with tbl1 as (
select 'ID1' as Criteria1, 'NID1' as Criteria2
union
select 'ID1', 'NID2'
union
select 'ID2', 'NID4'
union
select 'ID2', 'NID5'
union
select 'ID3', 'NID7'
)
select
rank() over (order by Criteria1) + DENSE_ranK() OVER (ORDER BY CRITERIA1) - 1 as C1,
rank() over (order by Criteria1) + row_number() over (partition by Criteria1 order by Criteria2) + DENSE_ranK() OVER (ORDER BY CRITERIA1) - 1 as C2,
Criteria1,
Criteria2
from
tbl1
稍微分解一下:
按照SQL 的说法,我们将每组 Criteria1 行称为 "partition"。
因此要求是:
C1 始终等于所有先前分区中的行数 + 当前分区的 1,加上先前分区的数量。
C2总是等于之前所有分区的行数+当前分区的1,加上之前的分区数,加上分区内所有之前的行数+当前行的1 .
RANK() over (order by Criteria1)
为您提供所有先前分区中的行数 + 1。
DENSE_RANK() over (order by Criteria1) - 1
给出之前分区的数量。
ROW_NUMBER() over (partition by Criteria1 order by Criteria2)
为您提供分区中之前的行数。