我有以下查询:
SELECT COLUMN1, COLUMN2, COUNT(*)
FROM TABLE
WHERE COLUMN3 IS NOT NULL
AND COLUMN4 = 1
AND COLUMN5 = 4
AND COLUMN6 = 43
AND COLUMN7 = $P{YEAR}
AND COLUMN8 = $P{IT}
GROUP BY COLUMN1, COLUMN2
ORDER BY COLUMN1 ASC
我想知道是否可以做类似的事情:
SELECT COLUMN1, COLUMN2, COUNT(*)
FROM TABLE
WHERE COLUMN3 IS NOT NULL
AND COLUMN4 = 1
AND COLUMN5 = 4
AND COLUMN6 = 43
AND COLUMN7 = $P{YEAR}
IF ($P{IT} != 'ALL') { AND COLUMN8 = $P{IT} }
GROUP BY COLUMN1, COLUMN2
ORDER BY COLUMN1 ASC
换句话说,我想添加到where子句" AND COLUMN8 = $ P {IT}"只有" $ P {IT}"价值不是" ALL"。这意味着如果报告必须按列" COLUMN8"或不。
有人知道这是否可行?还有其他方法可以完成这项工作吗?
我尝试执行上述查询,但我得到了编译运行时间'。
提前致谢。
答案 0 :(得分:1)
是的,您可以在iReport中执行此操作。但是你需要稍微区别一点。你需要第二个参数。保留$ P {IT},并添加$ P {IT_SQL}。给$ P {IT_SQL}一个默认值,如下所示:
$P{IT}.equals("ALL") ? "" : " AND COLUMN8 = '" + $P{IT} + "'"
然后您的查询应如下所示:
SELECT COLUMN1, COLUMN2, COUNT(*)
FROM TABLE
WHERE COLUMN3 IS NOT NULL
AND COLUMN4 = 1
AND COLUMN5 = 4
AND COLUMN6 = 43
AND COLUMN7 = $P{YEAR}
$P!{IT_SQL}
GROUP BY COLUMN1, COLUMN2
ORDER BY COLUMN1 ASC
当$ P {IT}的值为“ALL”时,这将为您提供所需的SQL(无!),当$ P {IT}拥有时,它将为您提供所需的SQL(COLUMN8 ='abc')值“abc”。