将参数传递给Jasper Reports的问题

时间:2014-12-17 05:04:57

标签: java mysql sql parameters jasper-reports

我正在尝试将2个参数传递给Jasper报告并获得结果。在SQL查询中,我将它们与查询组合如下。

SELECT 
ROUND(SUM(case when Updated_Date BETWEEN $P{secondYear}+"-03-01" AND $P{secondYear}+"-03-31" then Amount else 0 end),2) AS `March` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{secondYear}+"-02-01" AND $P{secondYear}+"-02-29" then Amount else 0 end),2) AS `February` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{secondYear}+"-01-01" AND $P{secondYear}+"-01-31" then Amount else 0 end),2) AS `January` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-12-01" AND $P{firstYear}+"-12-31" then Amount else 0 end),2) AS `December` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-11-01" AND $P{firstYear}+"-11-31" then Amount else 0 end),2) AS `November` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-10-01" AND $P{firstYear}+"-10-31" then Amount else 0 end),2) AS `October` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-09-01" AND $P{firstYear}+"-09-31" then Amount else 0 end),2) AS `September` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-08-01" AND $P{firstYear}+"-08-31" then Amount else 0 end),2) AS `August` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-07-01" AND $P{firstYear}+"-07-31" then Amount else 0 end),2) AS `July` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-06-01" AND $P{firstYear}+"-06-31" then Amount else 0 end),2) AS `June`, 
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-05-01" AND $P{firstYear}+"-05-31" then Amount else 0 end),2) AS `May`,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}+"-04-01" AND $P{firstYear}+"-04-31" then Amount else 0 end),2) AS `April` 
FROM VAT

然而,在我的Jasper报告中,我得到的是所有月份的增值税0。我做错了什么?

1 个答案:

答案 0 :(得分:1)

我找到了答案。在jasper报告中,如果要在SQL查询中组合parameters之类的String,则不应使用+符号。所以正确的代码如下。

SELECT 
ROUND(SUM(case when Updated_Date BETWEEN $P{secondYear}"-03-01" AND $P{secondYear}"-03-31" then Amount else 0 end),2) AS `March` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{secondYear}"-02-01" AND $P{secondYear}"-02-29" then Amount else 0 end),2) AS `February` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{secondYear}"-01-01" AND $P{secondYear}"-01-31" then Amount else 0 end),2) AS `January` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-12-01" AND $P{firstYear}"-12-31" then Amount else 0 end),2) AS `December` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-11-01" AND $P{firstYear}"-11-31" then Amount else 0 end),2) AS `November` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-10-01" AND $P{firstYear}"-10-31" then Amount else 0 end),2) AS `October` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-09-01" AND $P{firstYear}"-09-31" then Amount else 0 end),2) AS `September` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-08-01" AND $P{firstYear}"-08-31" then Amount else 0 end),2) AS `August` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-07-01" AND $P{firstYear}"-07-31" then Amount else 0 end),2) AS `July` ,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-06-01" AND $P{firstYear}"-06-31" then Amount else 0 end),2) AS `June`, 
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-05-01" AND $P{firstYear}"-05-31" then Amount else 0 end),2) AS `May`,
ROUND(SUM(case when Updated_Date BETWEEN $P{firstYear}"-04-01" AND $P{firstYear}"-04-31" then Amount else 0 end),2) AS `April` 
FROM VAT

如果您使用的是Jaspersoft Studio,它会在查询编辑器中将其显示为错误。但是,它没有任何问题。