I need to draw a chart in Jasper iReport that can sum the results based on user selection. Let's say I have the fruits table:
id (pk) town bananas orange cherry
1 boston 5 0 11
2 paris 100 18 12
3 bucharest 10 3 9
4 barcelona 9 4 12
From a Jasper web server input control with an ireport parameter associated (java.util.collection) user will select the fruit/fruits.
I need to sum all the fruits selected by the user and plot the chart.
For example, when then user selects both 'bananas' and 'oranges' the result would be
town result
1 boston 5
2 paris 118
3 bucharest 13
4 barcelona 13
The problem is:
I can't do
select sum(bananas,orange) as result from fruits group by town
.
mysql sum function doesn't work this way, but this is the way jasper input control return the user selection back to report query.
I've tried SELECT (SELECT REPLACE('bananas,orange', ',', '+')) as result from fruits group by town
, but MySQL doesn't recognize the replace statement as column name.
I get this:
town result
1 boston bananas+orange
2 paris bananas+orange
3 bucharest bananas+orange
4 barcelona bananas+orange
also, SELECT sum(SELECT REPLACE('bananas,orange', ',', '+')) as result from fruits group by town
will return 0 as result
Any idea? Maybe this can be done from ireport..?