Here is sample table of data
ID MC BC TIME AB AT
1 4 10 2016-12-05 09:02:00 5 8
2 4 20 2016-12-15 09:03:00 2 3
3 4 10 2016-12-15 09:02:00 1 4
4 4 20 2016-12-25 09:02:00 3 6
5 4 05 2016-12-05 09:02:00 4 2
6 4 05 2016-12-08 09:02:00 6 2
7 4 10 2016-12-11 09:02:00 7 6
8 4 10 2016-12-05 09:02:00 9 8
9 4 10 2016-12-15 09:02:00 9 8
10 4 10 2016-12-15 09:05:00 10 20
11 5 10 2016-12-15 09:05:00 10 20
12 5 10 2016-12-15 09:05:00 10 20
13 5 10 2016-12-15 09:05:00 10 20
If i query for where 'MC'= 4 and 'TIME' like 2016-12%
then i want data of 'AB' and 'AT' column merged based on day in 'TIME' column and each 'BC' have separate day merged.
Like in sample output day 15 have two 'BC' (10,20) and they have two different rows.
I want the output like this
ID MC BC TIME AB AT
1 4 10 2016-12-05 14 16
2 4 10 2016-12-11 7 6
3 4 10 2016-12-15 20 32
4 4 20 2016-12-15 2 3
5 4 20 2016-12-25 3 6
6 4 05 2016-12-05 4 2
7 4 05 2016-12-08 6 2
Table have more 2 million rows for current month like in sample table.
SELECT MC,
BC,
DATE(`TIME`) AS 'Day'
SUM(AB) AS AB,
SUM(AT) AS AT
FROM tbl
WHERE `TIME` >= '2016-12-01`
AND `TIME` < '2016-12-01` + INTERVAL 1 MONTH
AND MC = 4
GROUP BY MC, BC, DATE(`TIME`);
This would be useful:
INDEX(MC, `TIME`)