21

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?

2 Answers 2

37

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.

6
  • 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, 2011 at 10:34
  • @onedaywhen: well, that's the difference between theory and practice ;)
    – user1822
    Dec 15, 2011 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, 2011 at 11:16
  • @onedaywhen: so you mean I should change my wording? Which word would you recommend to express combination of two (column) values in SQL then? They don't necessarily need to be from the same table nor be a complete row.
    – user1822
    Dec 15, 2011 at 11:20
  • 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, 2011 at 13:23
16

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
2
  • 2
    Nice demonstration!
    – user1822
    Dec 14, 2011 at 15:07
  • I have to get the reflex to think about 'level', it's very usefull!
    – Luc M
    Jan 27, 2013 at 3:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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