我确实看到有类似问题的解决方案,但我无法让他们在我的方案中工作。 我在使用LISTAGG函数时返回重复的代码。 现在回来:
SELECT
,CD.CLAIM
,CD.CLAIMLN
,CD.PROV_INVOICE_UNTS
,CD.APPR_UNTS
,CD.PROV_INVOICE_AMT
,CD.PROV_CNTRCT_AMT
,CD.PLAN_CNTRCT_AMT as PLAN_AMT
,LISTAGG(DX.DIAG_CD,', ') WITHIN GROUP (ORDER BY DX.LVL_CD) AS DX_CODES
FROM CLAIM_DETAIL CD
INNER JOIN PATIENT_INTAKE_PLAN PIP
ON CD.PAT_NBR = PIP.PAT_NBR AND CD.ITK_ID = PIP.ITK_ID
INNER JOIN HEALTH_PLAN HP
ON HP.PLAN_ID = PIP.PLAN_ID
INNER JOIN PROVIDER_CCXPORTAL PR
ON PR.PROV_ID = CD.PROV_ID
INNER JOIN PROVIDER_PARENT PRP
ON PR.PROV_PRNT_ID = PRP.PROV_PRNT_ID
INNER JOIN PATIENT_CCXPORTAL PTP
ON PTP.PAT_NBR = CD.PAT_NBR
INNER JOIN CLAIM C
ON C.CLM_ID = CD.CLM_ID
LEFT JOIN CLAIM_DIAGNOSIS DX
ON CD.CLM_ID = DX.CLM_ID
WHERE
C.RCPT_DT >= '01-JUL-2014'
答案 0 :(得分:1)
使用正则表达式去掉重复项
....as PLAN_AMT,
RTRIM(
REGEXP_REPLACE(
(listagg(DX.DIAG_CD,',') WITHIN GROUP (ORDER BY DX.LVL_CD) ),
'([^,]*)(,\1)+($|,)',
'\1\3'),
',') AS DX_CODES
FROM......
答案 1 :(得分:1)
如果每个声明的DX_CODES非常多,则您的字符串可能超过SQL varchar2的最大长度。
你能试试吗?
SELECT
,CD.CLAIM
,CD.CLAIMLN
,CD.PROV_INVOICE_UNTS
,CD.APPR_UNTS
,CD.PROV_INVOICE_AMT
,CD.PROV_CNTRCT_AMT
,CD.PLAN_CNTRCT_AMT as PLAN_AMT
, (SELECT listagg(dx.diag_cd,',') within group ( order by dx.lvl_cd, dx.diag_cd ) FROM ( SELECT distinct clm_id, lvl_cd, diag_cd FROM claim_diagnosis ) dx WHERE dx.clm_id = cd.clm_id ) dx_codes
--,LISTAGG(DX.DIAG_CD,', ') WITHIN GROUP (ORDER BY DX.LVL_CD) AS DX_CODES
FROM CLAIM_DETAIL CD
INNER JOIN PATIENT_INTAKE_PLAN PIP
ON CD.PAT_NBR = PIP.PAT_NBR AND CD.ITK_ID = PIP.ITK_ID
INNER JOIN HEALTH_PLAN HP
ON HP.PLAN_ID = PIP.PLAN_ID
INNER JOIN PROVIDER_CCXPORTAL PR
ON PR.PROV_ID = CD.PROV_ID
INNER JOIN PROVIDER_PARENT PRP
ON PR.PROV_PRNT_ID = PRP.PROV_PRNT_ID
INNER JOIN PATIENT_CCXPORTAL PTP
ON PTP.PAT_NBR = CD.PAT_NBR
INNER JOIN CLAIM C
ON C.CLM_ID = CD.CLM_ID
--LEFT JOIN CLAIM_DIAGNOSIS DX
--ON CD.CLM_ID = DX.CLM_ID
WHERE
C.RCPT_DT >= '01-JUL-2014'
确保CLAIM_DIAGNOSIS.CLM_ID上有索引。