CTE中的De-Dupe行基于sql中的一个键

时间:2018-04-10 19:31:04

标签: sql oracle duplicates

我不确定我的问题需要什么。在下面的CTE中,我得到一个输出,它给了我想要的所有列,但我得到重复行,因为一个成员可以有两种不同类型的诊断代码,我想要的是每个成员ID有一行然后沿着例如,使用DX代码......

MEMBERID    FIRST_NAME     BIRTHDATE    DIAGNOSIS_CODE
999999999   John            9/9/9999        E11.9
999999999   John            9/9/9999        E79.8

我希望输出像这样

MEMBERID    FIRST_NAME     BIRTHDATE    DIAGNOSIS_CODE DIAGNOSIS_CODE
    999999999   John            9/9/9999        E11.9        E79.8

现在看到每个成员一行,这是我的代码,它运行没有任何问题,我只是想让每个成员获得一行。上面的例子仅仅是我的代码中没有实际的示例,但认为更容易阅读示例,而不是我的代码。非常感谢任何帮助

WITH

Dates as ( Select  ADD_MONTHS(TRUNC(sysdate,'MM'),-12) as MONTH1_BEGINDATE,
       LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE,'MM'),-1)) MONTH12_LASTDATE

         from dual   ---- requestor wants a rolling 12 month period         
), --select * from Dates

DX AS (Select  dx.diag_cd as Diagnosis_code, --- pulling in all members who have Diabetes DX regardless of date span from tab6 in ICUE for AZ
dx.mbr_id

From icue.mbr_diag dx

where dx.diag_cd in ('E08.00','E08.01','E08.10','E08.11','E08.21','E08.22','E08.29','E08.311','E08.319','E08.321',
'E08.329','E08.331','E08.339','E08.341','E08.349','E08.351','E08.352','E08.353','E08.354','E08.355',
'E08.359','E08.36','E08.37','E08.39','E08.40','E08.41','E08.42','E08.43','E08.44','E08.49',
'E08.51','E08.52','E08.59','E08.610','E08.618','E08.620','E08.621','E08.622','E08.628','E08.630',
'E08.638','E08.641','E08.649','E08.65','E08.69','E08.8','E08.9','E10.10','E10.11','E10.21',
'E10.22','E10.29','E10.311','E10.319','E10.321','E10.329','E10.331','E10.339','E10.341','E10.349',
'E10.351','E10.352','E10.353','E10.354','E10.355','E10.359','E10.36','E10.37','E10.39','E10.40',
'E10.41','E10.42','E10.43','E10.44','E10.49','E10.51','E10.52','E10.59','E10.610','E10.618',
'E10.620','E10.621','E10.622','E10.628','E10.630','E10.638','E10.641','E10.649','E10.65','E10.69',
'E10.8','E10.9','E11.00','E11.01','E11.10','E11.11','E11.21','E11.22','E11.29','E11.311',
'E11.319','E11.321','E11.329','E11.331','E11.339','E11.341','E11.349','E11.351','E11.352','E11.353',
'E11.354','E11.355','E11.359','E11.36','E11.37','E11.39','E11.40','E11.41','E11.42','E11.43',
'E11.44','E11.49','E11.51','E11.52','E11.59','E11.610','E11.618','E11.620','E11.621','E11.622',
'E11.628','E11.630','E11.638','E11.641','E11.649','E11.65','E11.69','E11.8', 'E11.9','E13.00',
'E13.01','E13.10','E13.11','E13.21','E13.22','E13.29','E13.311','E13.319','E13.321','E13.329',
'E13.331','E13.339','E13.341','E13.349','E13.351','E13.352','E13.353','E13.354','E13.355','E13.359',
'E13.36','E13.37','E13.39','E13.40','E13.41','E13.42','E13.43','E13.44','E13.49','E13.51',
'E13.52','E13.59','E13.610','E13.618','E13.620','E13.621','E13.622','E13.628','E13.630','E13.638',
'E13.641','E13.649','E13.65','E13.69','E13.8','E13.9') ---- all dx Diabetes code, pulling per request 

),  --select * From DX

members AS (

Select distinct -- need to dedup  by member ID
max(case when d.mbr_id_typ_id = 2 then d.mbr_id_txt end) over (partition by d.mbr_id) as MemberID,
max(case when d.mbr_id_typ_id = 1 then d.mbr_id_txt end) over (partition by d.mbr_id) as SubscriberID,
max(case when d.mbr_id_typ_id = 3 then d.mbr_id_txt end) over (partition by d.mbr_id) as MemberAlternateID,   
max(case when d.mbr_id_typ_id = 6 then d.mbr_id_txt end) over (partition by d.mbr_id) as MedicaidRecipientNumber,
mb.fst_nm as first_name,
mb.lst_nm as last_name,
trunc(mb.bth_dt) as birthdate,
dx.Diagnosis_code

from DX dx

inner join dates dd
on 1=1 

inner join icue.mbr mb
on dx.mbr_id = mb.mbr_id


inner join icue.mbr_cov c -- pull in dates
on mb.mbr_id = c.mbr_id

inner join icue.mbr_id d -- member identifier 
on dx.mbr_id = d.mbr_id
and d.mbr_id_typ_id in ('2','3','1','6')


and c.pol_iss_st_cd ='AZ' ---- Policy state lmiting to AZ only 
and (c.lob_typ_id='12' OR c.clm_pltfm_id='A9')  ---Community and state

),  --select * from members

Members_with_diabetesDX AS (
Select m.*,dd.MONTH1_BEGINDATE, dd.MONTH12_LASTDATE from members m
inner join dates dd on 1=1
) select * from Members_with_diabetesDX

1 个答案:

答案 0 :(得分:-1)

感谢大家的帮助。我继续使用row_number然后我使用ro_number做了Alias的标准。以下是我的代码。

 WITH

Dates as ( Select  ADD_MONTHS(TRUNC(sysdate,'MM'),-12) as MONTH1_BEGINDATE,
       LAST_DAY(ADD_MONTHS(TRUNC(SYSDATE,'MM'),-1)) MONTH12_LASTDATE

         from dual   ---- requestor wants a rolling 12 month period 

), --select * from Dates

DX AS (Select  dx.diag_cd as Diagnosis_code, --- pulling in all members who have Diabetes DX regardless of date span from tab6 in ICUE for AZ
dx.mbr_id

From icue.mbr_diag dx

where dx.diag_cd in ('E08.00','E08.01','E08.10','E08.11','E08.21','E08.22','E08.29','E08.311','E08.319','E08.321',
'E08.329','E08.331','E08.339','E08.341','E08.349','E08.351','E08.352','E08.353','E08.354','E08.355',
'E08.359','E08.36','E08.37','E08.39','E08.40','E08.41','E08.42','E08.43','E08.44','E08.49',
'E08.51','E08.52','E08.59','E08.610','E08.618','E08.620','E08.621','E08.622','E08.628','E08.630',
'E08.638','E08.641','E08.649','E08.65','E08.69','E08.8','E08.9','E10.10','E10.11','E10.21',
'E10.22','E10.29','E10.311','E10.319','E10.321','E10.329','E10.331','E10.339','E10.341','E10.349',
'E10.351','E10.352','E10.353','E10.354','E10.355','E10.359','E10.36','E10.37','E10.39','E10.40',
'E10.41','E10.42','E10.43','E10.44','E10.49','E10.51','E10.52','E10.59','E10.610','E10.618',
'E10.620','E10.621','E10.622','E10.628','E10.630','E10.638','E10.641','E10.649','E10.65','E10.69',
'E10.8','E10.9','E11.00','E11.01','E11.10','E11.11','E11.21','E11.22','E11.29','E11.311',
'E11.319','E11.321','E11.329','E11.331','E11.339','E11.341','E11.349','E11.351','E11.352','E11.353',
'E11.354','E11.355','E11.359','E11.36','E11.37','E11.39','E11.40','E11.41','E11.42','E11.43',
'E11.44','E11.49','E11.51','E11.52','E11.59','E11.610','E11.618','E11.620','E11.621','E11.622',
'E11.628','E11.630','E11.638','E11.641','E11.649','E11.65','E11.69','E11.8', 'E11.9','E13.00',
'E13.01','E13.10','E13.11','E13.21','E13.22','E13.29','E13.311','E13.319','E13.321','E13.329',
'E13.331','E13.339','E13.341','E13.349','E13.351','E13.352','E13.353','E13.354','E13.355','E13.359',
'E13.36','E13.37','E13.39','E13.40','E13.41','E13.42','E13.43','E13.44','E13.49','E13.51',
'E13.52','E13.59','E13.610','E13.618','E13.620','E13.621','E13.622','E13.628','E13.630','E13.638',
'E13.641','E13.649','E13.65','E13.69','E13.8','E13.9') ---- all dx Diabetes code, pulling per request 

),  --select * From DX

members AS (

Select distinct -- need to dedup  by member ID
max(case when d.mbr_id_typ_id = 2 then d.mbr_id_txt end) over (partition by d.mbr_id) as MemberID,
max(case when d.mbr_id_typ_id = 1 then d.mbr_id_txt end) over (partition by d.mbr_id) as SubscriberID,
max(case when d.mbr_id_typ_id = 3 then d.mbr_id_txt end) over (partition by d.mbr_id) as MemberAlternateID,   
max(case when d.mbr_id_typ_id = 6 then d.mbr_id_txt end) over (partition by d.mbr_id) as MedicaidRecipientNumber,
mb.fst_nm as first_name,
mb.lst_nm as last_name,
trunc(mb.bth_dt) as birthdate,
dx.Diagnosis_code

from DX dx

inner join dates d
on 1=1 

inner join icue.mbr mb
on dx.mbr_id = mb.mbr_id
--and mb.orig_sys_mbr_id in ('2','3')

inner join icue.mbr_cov c -- pull in dates
on mb.mbr_id = c.mbr_id

inner join icue.mbr_id d -- member identifier 
on dx.mbr_id = d.mbr_id
and d.mbr_id_typ_id in ('2','3','1','6')

and c.pol_iss_st_cd ='AZ' ---- Policy state lmiting to AZ only 
and (c.lob_typ_id='12' OR c.clm_pltfm_id='A9')  ---Community and state

),  --select * from members

Members_with_diabetesDX AS (
Select 
m.MemberID,
m.SubscriberID,
m.MemberAlternateID,   
m.MedicaidRecipientNumber,
m.first_name,
m.last_name,
m.birthdate,
m.Diagnosis_code,
row_number() over (partition by m.MemberID order by m.Diagnosis_code Desc) as Rank,
d.MONTH1_BEGINDATE, 
d.MONTH12_LASTDATE 

from members m
inner join dates d on 1=1
),-- select * from Members_with_diabetesDX 

Final as ( 
Select 
ff.MemberID,
ff.SubscriberID,
ff.MemberAlternateID,   
ff.MedicaidRecipientNumber,
ff.first_name,
ff.last_name,
ff.birthdate,
max(case when ff.Rank = 1 then ff.Diagnosis_code end) over (partition by ff.MemberID) as firstdx,
max(case when ff.Rank = 2 then ff.Diagnosis_code end) over (partition by ff.MemberID) as secdx,
max(case when ff.Rank = 3 then ff.Diagnosis_code end) over (partition by ff.MemberID) as thirddx,
max(case when ff.Rank = 4 then ff.Diagnosis_code end) over (partition by ff.MemberID) as fourthdx,
ff.MONTH1_BEGINDATE, 
ff.MONTH12_LASTDATE,
row_number() over (partition by ff.MemberID order by ff.Diagnosis_code,ff.SubscriberID,ff.MemberAlternateID,ff.MedicaidRecipientNumber,ff.first_name,ff.last_name,ff.birthdate Desc) as uniquerow

from Members_with_diabetesDX ff



) select * from Final where uniquerow ='1'