Hi,
I use ROLLUP MySQL for generates additional rows in the result set that represent subtotals and grand totals for the grouped data.
The grand total is calculated for all the data in the result set
My problem is in the last row of result set because on the last row of the second column is repetead the previous row, instead null value.
On the last row of first column I have instead correctly the Total value.
Can you help, please?
My code below
> mysql> SELECT
> COALESCE(AREA_Cod, 'Total') AREA_Cod,
> Area_name,
> SUM( CASE WHEN Event_date = '2024-04-01' THEN ROUND( rest, 0 ) ELSE 0 END ) AS '2024-04-01',
> SUM( CASE WHEN Event_date = '2024-04-02' THEN ROUND( rest, 0 ) ELSE 0 END ) AS '2024-04-02',
> SUM( CASE WHEN Event_date = '2024-04-03' THEN ROUND( rest, 0 ) ELSE 0 END ) AS '2024-04-03'
> FROM
> `do_table`
> WHERE
> Event_date BETWEEN '2024-04-01' AND '2024-04-03'
> GROUP BY
> AREA_Cod WITH ROLLUP;
>
> +----------+-----------------------------------+------------+------------+------------+
> | AREA_Cod | Area_name | 2024-04-01 | 2024-04-02 | 2024-04-03 |
> +----------+-----------------------------------+------------+------------+------------+
> | D7XX | AREA AAA | 10 | 0 | 0 |
> | D8XX | AREA BBB | 0 | 0 | 7 |
> | DEXX | AREA CCC | 0 | 0 | 0 |
> | DJXX | AREA DDD | 0 | 3 | 0 |
> | DKXX | AREA EEE | 0 | 0 | 0 |
> | DLXX | AREA FFF | 0 | 0 | 0 |
> | DNXX | AREA GGG | 0 | 0 | 0 |
> | DUXX | AREA HHH | 0 | 0 | 4 |
> | DVXX | AREA LLL | 0 | 0 | 0 |
> | DWXX | AREA MMM | 0 | 0 | 0 |
> | DXXX | AREA NNN | 0 | 0 | 0 |
> | DYXX | AREA PPP | 0 | 0 | 0 |
> | Total | AREA PPP | 10 | 3 | 11 |
> +----------+-----------------------------------+------------+------------+------------+
> 13 rows in set (0.40 sec)
I need this return
> +----------+-----------------------------------+------------+------------+------------+
> | AREA_Cod | Area_name | 2024-04-01 | 2024-04-02 | 2024-04-03 |
> +----------+-----------------------------------+------------+------------+------------+
> | D7XX | AREA AAA | 10 | 0 | 0 |
> | D8XX | AREA BBB | 0 | 0 | 7 |
> | DEXX | AREA CCC | 0 | 0 | 0 |
> | DJXX | AREA DDD | 0 | 3 | 0 |
> | DKXX | AREA EEE | 0 | 0 | 0 |
> | DLXX | AREA FFF | 0 | 0 | 0 |
> | DNXX | AREA GGG | 0 | 0 | 0 |
> | DUXX | AREA HHH | 0 | 6 | 4 |
> | DVXX | AREA LLL | 0 | 0 | 0 |
> | DWXX | AREA MMM | 0 | 0 | 1 |
> | DXXX | AREA NNN | 0 | 0 | 0 |
> | DYXX | AREA PPP | 0 | 0 | 0 |
> | Total | | 10 | 9 | 15 |
> +----------+-----------------------------------+------------+------------+------------+