Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have been getting myself very confused.

Could somebody kindly explain under what circumstances I would want to use a GROUP BY COALESCE?

My guess is that I would use it if I wanted to conditionally group a set of data by column B (if B was not null) and by column A otherwise. Does that sound right?

share|improve this question
add comment

2 Answers 2

up vote 32 down vote accepted

With GROUP BY b,a the tuples (null, 1), (1,1), (2,1) and (17,1) would end up in four different groups.

With GROUP BY coalesce(b,a) the tuples (null,1), (1,1), (2,1) and (17,1) would end up in the same group.

If you want the "conditional" grouping, then yes, the version with coalesce is probably what you want.

share|improve this answer
1  
Brilliant explanation, Many thanks! –  Mark McLaren Dec 14 '11 at 11:46
    
Chris Date: "A 'type' that contains a null isn't a type (because types contain values). A 'tuple' that contains a null isn't a tuple (because tuples contain values)." –  onedaywhen Dec 15 '11 at 10:34
    
@onedaywhen: well, that's the difference between theory and practice ;) –  a_horse_with_no_name Dec 15 '11 at 10:42
    
My point: that's the difference between a tuple in a relation and a row in a SQL table expression. A tuple doesn't apply to SQL, both in theory and in practice. –  onedaywhen Dec 15 '11 at 11:16
1  
For example, in Tutorial D, TUPLE { a 17 , b 1 } is the same as TUPLE { b 1 , a 17 }, but in SQL the row value constructor (17, 1) is not the same as the row value constructor (1, 17). This is why your "pairs" are not tuples. Because you have omitted a row type constructor I must assume from the context they are (a, b) rather than (b, a) but its inclusion would still not make it a tuple. In contrast, TUPLE { 17 , 1 } is not a valid tuple invocation in Tutorial D, nor is TUPLE { a null , b 1 }. –  onedaywhen Dec 15 '11 at 13:23
show 2 more comments

Here is a demonstration of a_horse_with_no_name's excellent+1 answer.

SQL> WITH Data AS (
  2     SELECT level, DECODE(Level,3,NULL,1) A
  3        , DECODE(level,2,NULL,4,2,1) B
  4     FROM dual connect by level <=5
  5     )
  6  SELECT A, B, count(*) FROM Data GROUP BY B, A;

A B   COUNT(*)
- - ----------
1 1          2
1            1
1 2          1
  1          1


SQL> WITH Data AS (
  2     SELECT level, DECODE(Level,3,NULL,1) A
  3        , DECODE(level,2,NULL,4,2,1) B
  4     FROM dual connect by level <=5
  5     )
  6  SELECT COALESCE(B, A) X, count(*) FROM Data GROUP BY COALESCE(B, A);

X   COUNT(*)
- ----------
1          4
2          1
share|improve this answer
2  
Nice demonstration! –  a_horse_with_no_name Dec 14 '11 at 15:07
    
I have to get the reflex to think about 'level', it's very usefull! –  Luc M Jan 27 '13 at 3:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.