I’m having a problem with a query containing a GROUP BY. As part of diagnosing it I removed the GROUP BY statement in order to see the raw data – however when I do that I get FEWER rows returned that I do with the GROUP BY.
To reproduce:
CREATE TABLE `test1` (
`date` bigint(20) DEFAULT NULL,
`quantity` int(11) DEFAULT NULL,
`processed` int(11) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
INSERT INTO `test1` VALUES (1312483084,3,2),(1312483084,1,2),(1312483148,1,2),(1312483148,1,2),(1314038654,1,2),(1314301805,1,2);
Now run my original SELECT (Complete with GROUP BY):
SELECT DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510)) DIV 28 AS date_idx,
min(DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510))) AS orig_date_idx, SUM(test1.quantity) AS num_items,
IF(test1.processed in (2,3,4), 'Complete','Pending') as status
FROM test1
GROUP BY status, date_idx \G
You should get two rows returned:
*************************** 1. row ***************************
date_idx: 0
orig_date_idx: 8
num_items: 7
status: Complete
*************************** 2. row ***************************
date_idx: 1
orig_date_idx: 29
num_items: 1
status: Complete
Now remove the GROUP BY, ie:
SELECT DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510)) DIV 28 AS date_idx,
min(DATEDIFF(from_unixtime(test1.date), from_unixtime(1311801510))) AS orig_date_idx, SUM(test1.quantity) AS num_items,
IF(test1.processed in (2,3,4), 'Complete','Pending') as status
FROM test1
All of a sudden you only get one row returned – the row that had date_idx == 1 in the original result has been merged into the entry for date_idx == 0
*************************** 1. row ***************************
date_idx: 0
orig_date_idx: 8
num_items: 8
status: Complete
All ideas gratefully received!
Frankly I’m surprised that it runs without just giving you an error for using aggregate functions without a group by, maybe has something to do with the fact that all your fields are calculated values. Anyway, get rid of all your aggregate functions to get to the underlying data.