如果有3个拒绝代码,则当前查询为一个发票返回3行,如果有多个拒绝代码和多个拒绝日期,则返回更多。我正在尝试为每个拒绝代码创建一个列,以便所有结果都可以在一行上。客户端的要求是每个拒绝都在它自己的列中,所以我无法使用listagg函数。 结果应如下所示:
办公室,发票,拒绝日期,拒绝代码1,拒绝代码2,拒绝代码3,拒绝日期2,拒绝代码1 ......等等
Oracle数据库。目前的代码:
SELECT
A.OFFICE_NBR,
A.INV_NBR,
TO_DATE(A.CRTD_DT,'MM/DD/YYYY') AS CARC_DT,
A.CLM_ID,
A.CLM_LN_ID,
A.RSN_CD
FROM DENIALS A
WHERE A.OFFICE_NBR = '1234'
AND A.INV_NBR = '123456'
答案 0 :(得分:0)
我猜测了一些测试数据,但这应该可以帮到你。请添加一些前后数据,这有助于了解您尝试做什么,并允许构建更现实的解决方案。
无论如何,需要注意的是,您必须提前为每个可能的拒绝代码定义DENIAL_CD_X列。我将有兴趣看看是否有人可以为denial_cd列提出动态解决方案。
SQL> with DENIALS(OFFICE_NBR, INV_NBR, CRTD_DT, RSN_CD) as (
2 select '11', '1111', '07/31/2015', '1' from dual
3 union
4 select '11', '1111', '07/31/2015', '99' from dual
5 union
6 select '11', '1111', '07/31/2015', '50' from dual
7 union
8 select '11', '1113', '06/01/2014', '34' from dual
9 union
10 select '11', '1113', '06/01/2014', '71' from dual
11 union
12 select '32', '3232', '06/21/2015', '34' from dual
13 union
14 select '32', '3232', '07/31/2015', '99' from dual
15 )
16 select OFFICE_NBR, INV_NBR, TO_DATE(CRTD_DT,'MM/DD/YYYY') AS CARC_DT,
17 DENIAL_CD_1, DENIAL_CD_2, DENIAL_CD_3
18 from
19 (
20 select OFFICE_NBR, INV_NBR, CRTD_DT, rsn_cd,
21 row_number() over(partition by OFFICE_NBR, INV_NBR, CRTD_DT order by OFFICE_NBR, INV_NBR, CRTD_DT, rsn_cd) rn
22 from DENIALS
23 )
24 pivot
25 (
26 max(rsn_cd)
27 for rn in ('1' as DENIAL_CD_1, '2' as DENIAL_CD_2, '3' as DENIAL_CD_3)
28 )
29 order by OFFICE_NBR, INV_NBR, CRTD_DT;
OFFICE_NBR INV_NBR CARC_DT DENIAL_CD_1 DENIAL_CD_2 DENIAL_CD_3
---------- ------- ---------- ----------- ----------- -----------
11 1111 31-JUL-15 1 50 99
11 1113 01-JUN-14 34 71
32 3232 21-JUN-15 34
32 3232 31-JUL-15 99
SQL>