Sql 当select不返回任何行时,是否有办法使用占位符?
我有一个表,Sql 当select不返回任何行时,是否有办法使用占位符?,sql,postgresql,jasper-reports,Sql,Postgresql,Jasper Reports,我有一个表,report\u total,其中包含一些total\u类型\u cd(代码)的计算值,但不一定包含所有类型 如果没有相应的行,我希望在select中有一个占位符,以便重命名的总金额(unitem\u cntrib/total\u contrib..)的值为0,并且即使找不到值,我始终会得到8个项目的返回。我想,COALESCE函数可能会起作用,但我无法编写一个可以接受的查询 这些查询结果将进入pdf报告,因此我需要一些东西,即使它是0。现在,不会生成任何报告,因为如果all值不存在
report\u total
,其中包含一些total\u类型\u cd
(代码)的计算值,但不一定包含所有类型
如果没有相应的行,我希望在select中有一个占位符,以便重命名的总金额(unitem\u cntrib
/total\u contrib
..)的值为0
,并且即使找不到值,我始终会得到8个项目的返回。我想,COALESCE
函数可能会起作用,但我无法编写一个可以接受的查询
这些查询结果将进入pdf报告,因此我需要一些东西,即使它是0。现在,不会生成任何报告,因为如果all值不存在,则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
我认为您需要条件聚合:
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};