Bug #46465 | When IF is used with ROLLUP incorrect data is returned | ||
---|---|---|---|
Submitted: | 30 Jul 2009 3:34 | Modified: | 10 Feb 2018 17:54 |
Reporter: | Chris Hamono | Email Updates: | |
Status: | Not a Bug | Impact on me: | |
Category: | MySQL Server: DML | Severity: | S3 (Non-critical) |
Version: | 5.0.32, 5.1.38 | OS: | Linux (debian-etch) |
Assigned to: | CPU Architecture: | Any | |
Tags: | IF, with rollup |
[30 Jul 2009 3:34]
Chris Hamono
[30 Jul 2009 4:26]
Valeriy Kravchuk
Thank you for the problem report. Verified just as described with latest 5.1.38 from bzr: valeriy-kravchuks-macbook-pro:5.1 openxs$ bin/mysql -uroot test Reading table information for completion of table and column names You can turn off this feature to get a quicker startup with -A Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 3 Server version: 5.1.38-debug Source distribution Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql> SELECT `date`,IF(`day`,`date`,'total') as date2,SUM(`hours`) FROM `timesheets` WHERE -> `cost_centre_id`=22 GROUP BY `day` -> ; +------------+------------+--------------+ | date | date2 | SUM(`hours`) | +------------+------------+--------------+ | 2009-07-06 | 2009-07-06 | 15.00 | | 2009-07-07 | 2009-07-07 | 15.00 | | 2009-07-08 | 2009-07-08 | 15.00 | | 2009-07-09 | 2009-07-09 | 30.00 | | 2009-07-10 | 2009-07-10 | 15.00 | | 2009-07-13 | 2009-07-13 | 15.00 | | 2009-07-14 | 2009-07-14 | 15.00 | | 2009-07-15 | 2009-07-15 | 15.00 | | 2009-07-16 | 2009-07-16 | 15.00 | +------------+------------+--------------+ 9 rows in set (0.05 sec) mysql> SELECT `date`,IF(`day`,`date`,'total') as date2,SUM(`hours`) FROM `timesheets` WHERE `cost_centre_id`=22 GROUP BY `day` WITH ROLLUP;; +------------+------------+--------------+ | date | date2 | SUM(`hours`) | +------------+------------+--------------+ | 2009-07-06 | 2009-07-07 | 15.00 | | 2009-07-07 | 2009-07-08 | 15.00 | | 2009-07-08 | 2009-07-09 | 15.00 | | 2009-07-09 | 2009-07-10 | 30.00 | | 2009-07-10 | 2009-07-13 | 15.00 | | 2009-07-13 | 2009-07-14 | 15.00 | | 2009-07-14 | 2009-07-15 | 15.00 | | 2009-07-15 | 2009-07-16 | 15.00 | | 2009-07-16 | 2009-07-16 | 15.00 | | 2009-07-16 | total | 150.00 | +------------+------------+--------------+ 10 rows in set (0.02 sec)
[30 Jul 2009 4:54]
Giuseppe Maxia
It doesn't look like a bug to me. The query is grouping by *day*. If I add non aggregated columns, their value is undefined. Hence, in different runs, I can get different results.
[30 Jul 2009 6:08]
Chris Hamono
Thanks Giuseppe. I can confirm that if I use the aggregate function MAX() to select the date the function works as expected. using min and max... SELECT MAX(`date`),IF(`day`,MIN(`date`),'total') as date2,SUM(`hours`),`day` FROM `timesheets` WHERE `cost_centre_id`=22 GROUP BY `day` WITH ROLLUP As the `date` has the same value within the aggregated set I assumed it should return that value.
[30 Jul 2009 6:14]
Chris Hamono
I would just like to point out that without the "WITH ROLLUP" the results are as expected. ie: `date` returns the same value inside and outside of the IF statement.
[10 Feb 2018 17:54]
Roy Lyseng
Posted by developer: Original test case was using non-deterministic GROUP BY operation. Apparently not a bug. Please reopen if you have a test case that uses SQL mode ONLY_FULL_GROUP_BY.