pl/sql-将两行合并为一行

vecaoik1  于 2021-07-24  发布在  Java
关注(0)|答案(2)|浏览(681)

已编辑:更新要指定的查询。如何在查询中将两行合并为一行,如下所示:

SELECT  pp.polmain_key
                                           ,MAX(CASE WHEN sub_risk2.rown = 1 THEN sub_risk2.ptdcoverrequired00 ELSE NULL END)               ptdcoverrequired00
                                           ,MAX(CASE WHEN sub_risk2.rown = 2 THEN sub_risk2.ptdcoverrequired00 ELSE NULL END)               ptdcoverrequired01
                                           ,MAX(CASE WHEN sub_risk2.rown = 1 THEN sub_risk2.ptdfixedsumins00 ELSE NULL END)                 ptdfixedsumins00
                                           ,MAX(CASE WHEN sub_risk2.rown = 2 THEN sub_risk2.ptdfixedsumins00 ELSE NULL END)                 ptdfixedsumins01
                               FROM    table1 pp
                               LEFT JOIN    (SELECT  pp.polmain_key
                                                    ,MAX(CASE WHEN pab.question_key IN (  405) THEN pab.answer_text ELSE NULL END)  ptdcoverrequired00 
                                                    ,MAX(CASE WHEN pab.question_key IN (25338) THEN pab.answer_text ELSE NULL END)  ptdfixedsumins00 
                                                    ,pp.section_ref
                                                    ,pp.risk_ref
                                                    ,pp.sub_risk_ref
                                                    ,ROW_NUMBER() OVER (PARTITION BY pp.section_ref ORDER BY NULL) rown
                                                    ,COUNT(*) OVER (PARTITION BY pp.risk_ref) cnt
                                                    ,ROW_NUMBER() OVER (PARTITION BY pp.sub_risk_ref ORDER BY NULL) pos
                                             FROM    table1 pp
                                        LEFT JOIN    table2 pab
                                               ON    pp.policy_part_key = pab.policy_part_key
                                              AND    pab.question_key IN (25338,405)
                                            WHERE    pp.policy_part_level_key = 3
                                            AND      pp.product_sub_risk_key = 65724
                                            GROUP BY  pp.polmain_key
                                                     ,pp.section_ref
                                                     ,pp.risk_ref
                                                     ,pp.sub_risk_ref
                                            ORDER BY pp.polmain_key
                                                     ,pp.section_ref
                                                     ,pp.risk_ref
                                                     ,pp.sub_risk_ref
                                            ) sub_risk2
                                      ON    pp.risk_ref = sub_risk2.risk_ref
                                   WHERE    pp.policy_part_level_key = 2
                                   AND      pp.product_risk_key = 65722
                                   GROUP BY  pp.polmain_key
                                            ,sub_risk2.rown
                                            ,sub_risk2.ptdcoverrequired00
                                            ,sub_risk2.ptdfixedsumins00
                                   ORDER BY pp.polmain_key
                                   ;

我得到的结果是:

"POLMAIN_KEY"                 "PTDCOVERREQUIRED00"          "PTDCOVERREQUIRED01"          "PTDFIXEDSUMINS00"            "PTDFIXEDSUMINS01"            
    "1003245353"                  "Fixed Sum Insured (£)"       ""                            "3000.00"                     ""                            
    "1003245353"                  ""                            "Multiple of Salary"          ""                            "40.00"                            
    "1003267567"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""                            
    "1003267576"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""                            
    "1003270623"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""                            
    "1003270631"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""

我想实现这样的目标:

"POLMAIN_KEY"                 "PTDCOVERREQUIRED00"          "PTDCOVERREQUIRED01"          "PTDFIXEDSUMINS00"            "PTDFIXEDSUMINS01"            
    "1003245353"                  "Fixed Sum Insured (£)"       "Multiple of Salary"          "3000.00"                     "40.00"                            
    "1003267567"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""                            
    "1003267576"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""                            
    "1003270623"                  "Fixed Sum Insured (£)"       ""                            "50000.00"                    ""                            
    "1003270631"                  "Fixed Sum Insured (£)"       ""                            "50000.00"

有什么简单的方法可以修改现有的查询以返回这样的结果吗?

23c0lvtd

23c0lvtd1#

可以使用条件聚合。您的查询格式不正确,但看起来像这样:

SELECT pol_key,
       MAX(CASE WHEN seqnum = 1 THEN coumn100 END) as column100,
       MAX(CASE WHEN seqnum = 1 THEN coumn200 END) as column200,
       MAX(CASE WHEN seqnum = 1 THEN coumn300 END) as column300,
       MAX(CASE WHEN seqnum = 1 THEN coumn101 END) as column101,
       MAX(CASE WHEN seqnum = 1 THEN coumn201 END) as column201,
       MAX(CASE WHEN seqnum = 1 THEN coumn301 END) as column301
FROM (SELECT pol.*, rd2.*
             ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL) as seqnum
      FROM table pol JOIN
           rd2
           ON . . . 
     ) pol
GROUP BY pol_key
0ejtzxu1

0ejtzxu12#

您只需按pol\ u键分组,并对列使用聚合函数来除去空值,如下所示

select pol_key
      ,max(column100) as column100
      ,max(column200) as column200
      ,max(column300) as column300
      ,max(column101) as column101
      ,max(column201) as column201
      ,max(column301) as column301
 from (    
SELECT pol.pol_key
          ,DECODE(ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL),1,rd2.column100)                           column100
          ,DECODE(ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL),1,rd2.column200)                           column200
          ,DECODE(ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL),1,rd2.column300)                     column300
          ,DECODE(ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL),1,rd2.column101)                           column101
          ,DECODE(ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL),1,rd2.column201)                           column201
          ,DECODE(ROW_NUMBER() OVER (PARTITION BY pol.pol_key, RD2.POS ORDER BY NULL),1,rd2.column301)                     column301
     FROM table pol
     )x

相关问题