shadedecho
Programmer
OK... I am getting confused with how to JOIN a table to itself (1 or more times) to get "horizontally" different SUM()'s on a particular field...
mytable:
If I do:
SELECT `A`, SUM(`B`) AS 'Btotal' FROM `mytable` GROUP BY `A`
I get
BUT, what I would like is to have those same totals reported to me horizontally in one row... like:
My first thought is that this has to be done with joins... like joining the table to itself another one 3 times, each time with a different ON clause... so, first:
If I do a LEFT JOIN from Atable to mytable, ON A=A, like:
SELECT SUM(mytab.`B`) AS 'Btotal' FROM `Atable` atab LEFT JOIN `mytable` mytab ON atab.`A` = mytab.`A` GROUP BY atab.`A`
That would give me the three row-totals again, this time with a JOIN involved, so the SUM works correct over that one join...
So, to get it to put them all in a row, I figured I could just JOIN mytable to Atable 3 times, each time with a different ON condition...
SELECT SUM(mytable1.`B`) AS '1total',
SUM(mytable2.`B`) AS '2total',
SUM(mytable3.`B`) AS '3total'
FROM `Atable` Atable1
LEFT JOIN `mytable` mytable1 ON mytable1.`A` = 1 AND mytable1.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable2 ON mytable2.`A` = 2 AND mytable2.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable3 ON mytable3.`A` = 3 AND mytable3.`A` = Atable1.`A`
GROUP BY Atable1.`A`
I thought the temp table created after these joins but before the sum'ing would look something like:
And the result I get is closer to what I want, but still not one row in the result set...
Clearly, the GROUP BY is part of the problem, cause its creating the 3 rows. So, I thought maybe if I removed the GROUP BY it would bring it all down to one row, which it did, but this time the SUM'ing didn't work right at all. It added all 3 totals together, and put that in all 3 fields:
Lastly, I thought maybe I could just not have the "Atable" involved (notice below the aliases are named the same for consistency between the two queries, but now it refers to `mytable` all 4 times), and just join the table to itself 4 times (since the table itself has the correct `A` values in it already, like:
SELECT SUM(mytable1.`B`) AS '1total',
SUM(mytable2.`B`) AS '2total',
SUM(mytable3.`B`) AS '3total'
FROM `mytable` Atable1
LEFT JOIN `mytable` mytable1 ON mytable1.`A` = 1 AND mytable1.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable2 ON mytable2.`A` = 2 AND mytable2.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable3 ON mytable3.`A` = 3 AND mytable3.`A` = Atable1.`A`
GROUP BY Atable1.`A`
This produced nowhere near what I was wanting... the first 2 rows of the result set, the SUM was tripled, and the last row of the result set, the SUM was quadrupled. this makes no sense to me whatsoever.
And without the GROUP BY in that statement, it adds all 3 together, then triples that SUM for the first 2 fields and quadruples it for the third field:
<insert growl here> Any thoughts?
mytable:
Code:
+-----+-----+
| A | B |
+-----+-----+
| 1 | 13 |
| 1 | 3 |
| 2 | 6 |
| 1 | 1 |
| 3 | 5 |
| 2 | 5 |
+-----+-----+
If I do:
SELECT `A`, SUM(`B`) AS 'Btotal' FROM `mytable` GROUP BY `A`
I get
Code:
+-----+----------+
| A | Btotal |
+-----+----------+
| 1 | 17 |
| 2 | 11 |
| 3 | 5 |
+-----+----------+
BUT, what I would like is to have those same totals reported to me horizontally in one row... like:
Code:
+----------+----------+----------+
| 1total | 2total | 3total |
+----------+----------+----------+
| 17 | 11 | 5 |
+----------+----------+----------+
My first thought is that this has to be done with joins... like joining the table to itself another one 3 times, each time with a different ON clause... so, first:
Code:
Atable
+-----+
| A |
+-----+
| 1 |
| 2 |
| 3 |
+-----+
If I do a LEFT JOIN from Atable to mytable, ON A=A, like:
SELECT SUM(mytab.`B`) AS 'Btotal' FROM `Atable` atab LEFT JOIN `mytable` mytab ON atab.`A` = mytab.`A` GROUP BY atab.`A`
That would give me the three row-totals again, this time with a JOIN involved, so the SUM works correct over that one join...
So, to get it to put them all in a row, I figured I could just JOIN mytable to Atable 3 times, each time with a different ON condition...
SELECT SUM(mytable1.`B`) AS '1total',
SUM(mytable2.`B`) AS '2total',
SUM(mytable3.`B`) AS '3total'
FROM `Atable` Atable1
LEFT JOIN `mytable` mytable1 ON mytable1.`A` = 1 AND mytable1.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable2 ON mytable2.`A` = 2 AND mytable2.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable3 ON mytable3.`A` = 3 AND mytable3.`A` = Atable1.`A`
GROUP BY Atable1.`A`
I thought the temp table created after these joins but before the sum'ing would look something like:
Code:
+-----+------+------+------+
| 1 | 13 | null | null |
| 1 | 3 | null | null |
| 2 | null | 6 | null |
| 1 | 1 | null | null |
| 3 | null | null | 5 |
| 2 | null | 5 | null |
+-----+------+------+------+
And the result I get is closer to what I want, but still not one row in the result set...
Code:
+-----+-----+-----+
| 17 | 0 | 0 |
| 0 | 11 | 0 |
| 0 | 0 | 5 |
+-----+-----+-----+
Clearly, the GROUP BY is part of the problem, cause its creating the 3 rows. So, I thought maybe if I removed the GROUP BY it would bring it all down to one row, which it did, but this time the SUM'ing didn't work right at all. It added all 3 totals together, and put that in all 3 fields:
Code:
+-----+-----+-----+
| 33 | 33 | 33 |
+-----+-----+-----+
Lastly, I thought maybe I could just not have the "Atable" involved (notice below the aliases are named the same for consistency between the two queries, but now it refers to `mytable` all 4 times), and just join the table to itself 4 times (since the table itself has the correct `A` values in it already, like:
SELECT SUM(mytable1.`B`) AS '1total',
SUM(mytable2.`B`) AS '2total',
SUM(mytable3.`B`) AS '3total'
FROM `mytable` Atable1
LEFT JOIN `mytable` mytable1 ON mytable1.`A` = 1 AND mytable1.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable2 ON mytable2.`A` = 2 AND mytable2.`A` = Atable1.`A`
LEFT JOIN `mytable` mytable3 ON mytable3.`A` = 3 AND mytable3.`A` = Atable1.`A`
GROUP BY Atable1.`A`
This produced nowhere near what I was wanting... the first 2 rows of the result set, the SUM was tripled, and the last row of the result set, the SUM was quadrupled. this makes no sense to me whatsoever.
Code:
+-----+-----+-----+
| 51 | 0 | 0 |
| 0 | 33 | 0 |
| 0 | 0 | 20 |
+-----+-----+-----+
And without the GROUP BY in that statement, it adds all 3 together, then triples that SUM for the first 2 fields and quadruples it for the third field:
Code:
+-----+-----+-----+
| 99 | 99 | 132 |
+-----+-----+-----+
<insert growl here> Any thoughts?