Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distinguish between NULL's when using "group by ... with rollup"

When I run a query using group by ... with rollup:

select a, b, sum(c) 
from <table> 
group by a, b with rollup;

I get duplicate rows in (what I consider to be) the PK of the query (that is, the group-by columns):

+------+------+--------+
| a    | b    | sum(c) |
+------+------+--------+
| NULL | NULL |     13 |
| NULL |    1 |      4 |
| NULL |    3 |      8 |
| NULL |    4 |      9 |
| NULL | NULL |     34 |
|    1 |    3 |     17 |
|    1 |    4 |   NULL |
|    1 |   17 |      2 |
|    1 | NULL |     19 |
|    2 | NULL |      6 |
|    2 |    1 |     17 |
|    2 |    3 |     17 |
|    2 | NULL |     40 |
|    4 |   17 |      2 |
|    4 | NULL |      2 |
|    5 | NULL |     11 |
|    5 |    6 |      7 |
|    5 | NULL |     18 |
|   13 |    4 |      2 |
|   13 | NULL |      2 |
|   14 |   41 |      3 |
|   14 | NULL |      3 |
|   18 |    1 |      2 |
|   18 | NULL |      2 |
|   41 |    2 |     17 |
|   41 | NULL |     17 |

... more rows follow ...

How do I distinguish (NULL, NULL, 13) from (NULL, NULL, 34)? That is, how do I distinguish between the row that has nulls because of the underlying data, and the row that has nulls because it was added by rollup? (Note that there are more examples -- (2, NULL, 6) and (2, NULL, 40))

like image 653
Matt Fenwick Avatar asked Feb 08 '12 20:02

Matt Fenwick


1 Answers

Good question. One option I can think of is to do this:

select COALESCE(a, -1) AS a, COALESCE(b, -1) AS b, sum(c) 
from <table> 
group by COALESCE(a, -1), COALESCE(b, -1) with rollup;
like image 168
Cade Roux Avatar answered Sep 26 '22 21:09

Cade Roux