当select返回没有行时,有没有办法让占位符?

时间:2015-05-11 22:09:29

标签: sql postgresql jasper-reports

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

如果没有相应的行,我希望在选择中有占位符,以便renamed total_amtunitem_cntrib / total_contrib ...)具有值0即使没有找到任何值,我也总是得到8项的回报。我想也许COALESCE函数可能有用,但我无法编写一个可接受的查询。

这些查询结果将进入pdf报告,所以我想要一些东西,即使它是0.现在,没有生成报告,因为如果所有,则select不返回任何行不存在。下面是我的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

1 个答案:

答案 0 :(得分:1)

我认为你想要条件聚合:

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};