有没有办法在 select returns 没有行时使用占位符?

Is there a way to have a placeholder when select returns no rows?

我有一个 table、report_total,其中包含一些 total_types_cd(代码)的计算值,但不一定包含所有这些值。

如果没有相应的行,我想在 select 中有一个占位符,这样 renamed total_amt (unitem_cntrib/total_contrib... ) 的值为 0,我总是得到 8 个项目的 return,即使没有找到任何值。我认为 COALESCE 函数可能会起作用,但我无法编写 acceptable 的查询。

这些查询结果将进入 pdf 报告,所以我想要一些东西,即使它是 0。现在,没有生成任何报告,因为 select returns 没有行如果所有 值都不存在。下面是我的 select 语句,$P{ReportID} 被输入到报告生成器中。

SELECT  unitem_cntrib, total_cntrib, unitem_expnd, total_expnd,
        unitem_pldg, on_hand, tot_loan, unitem_loan
FROM
(select total_amt  from report_total where calculation_type_cd ='UNITEMIZED_PLUS_LUMPSUM' 
and total_type_cd = 'TOT_CNTRB' and report_info_id=$P{ReportID} ) AS  unitem_cntrib,
(select total_amt from report_total where calculation_type_cd ='GRANDTOTAL' 
and total_type_cd = 'TOT_CNTRB' and report_info_id=$P{ReportID} ) AS  total_cntrib,
(select total_amt from report_total where calculation_type_cd ='UNITEMIZED_PLUS_LUMPSUM' 
and total_type_cd = 'TOT_EXPND' and report_info_id=$P{ReportID} ) AS  unitem_expnd,
(select total_amt from report_total where calculation_type_cd ='GRANDTOTAL' 
and total_type_cd = 'TOT_EXPND' and report_info_id=$P{ReportID} ) AS  total_expnd,
(select total_amt from report_total where calculation_type_cd ='UNITEMIZED_PLUS_LUMPSUM' 
and total_type_cd = 'TOT_PLEDGE' and report_info_id=$P{ReportID} ) AS  unitem_pldg,
(select total_amt from report_total where calculation_type_cd ='LUMPSUM' 
and total_type_cd = 'TOT_CNTRB_BALANCE' and report_info_id=$P{ReportID} ) AS  on_hand,
(select total_amt  from report_total where calculation_type_cd ='LUMPSUM' 
and total_type_cd = 'TOT_LOAN_PRINCIPAL' and report_info_id=$P{ReportID} ) AS  tot_loan,
(select total_amt  from report_total where calculation_type_cd ='UNITEMIZED_PLUS_LUMPSUM' 
and total_type_cd = 'TOT_LOAN' and report_info_id=$P{ReportID} ) AS  unitem_loan

我想你想要条件聚合:

select max(case when calculation_type_cd = 'UNITEMIZED_PLUS_LUMPSUM' 
                 and total_type_cd = 'TOT_CNTRB' 
                then total_amt end) as unitem_cntrib,
       max(case when calculation_type_cd = 'GRANDTOTAL'
                 and total_type_cd = 'TOT_CNTRB' 
                then total_amt end) as total_cntrib,
       . . . 
 from report_total rt
where rt.report_info_id = $P{ReportID};