我试图让MySQL支持的Jasper报告在查询表达式中有一定的GROUP BY。我可以使用参数在两个不同的列之间切换:
SELECT id, city, state, zip, value1, value2, value3, IF($P{consolidate_zip}, zip, city) AS groupField
FROM table1
GROUP BY groupField
但是,在这种情况下,我需要在多列或单列之间切换。
SELECT id, city, state, zip, value1, value2, value3, IF($P{consolidate_loc}, "city, state, zip", city) AS groupField
FROM table1
GROUP BY groupField
我正在尝试将其放入现有的报表生成器中,因此我仅限于SQL表达式,属性和参数。
是否有办法利用报表属性或格式化表达式以允许进行此类分组?
答案 0 :(得分:0)
它可能不太漂亮,但您可以将它们连接在一起:
SELECT id, city, state, zip, value1, value2, value3,
IF($P{consolidate_loc}, concat(city, ', ', state, ' ', zip), city) AS groupField
FROM table1
GROUP BY groupField;
您已经拥有select
中的所有字段,因此您只需将表达式放在group by
中:
SELECT id, city, state, zip, value1, value2, value3
FROM table1
GROUP BY IF($P{consolidate_loc}, concat(city, ', ', state, ' ', zip), city)
如果任何值可能为NULL
,则需要替换它们(最好使用coalesce()
。