已编辑:更新要指定的查询。如何在查询中将两行合并为一行,如下所示:
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"
有什么简单的方法可以修改现有的查询以返回这样的结果吗?
2条答案
按热度按时间23c0lvtd1#
可以使用条件聚合。您的查询格式不正确,但看起来像这样:
0ejtzxu12#
您只需按pol\ u键分组,并对列使用聚合函数来除去空值,如下所示