SQL 2005 - Add Coalesce for Nulls?

Experts,

I use the below query to summarize multiple rows into a single row. Works good

select
	REF,
	DATE,
	LEV1,
	LEV2,
	MAX(FROM_CODE)as FROM_CODE,
	SUM(FROM_QTY)as FROM_QTY,
	MAX(TO_CODE)as TO_CODE,
	SUM(TO_QTY)as TO_QTY
	FROM BVF_947
	GROUP BY REF, DATE, LEV1, LEV2

Open in new window


Sometimes the FROM or TO columns have no value and are null. When there is no value, I want to default 'UNR'.  I have been playing with COALESCE, but many errors.
How Can I get the nulls to default to 'UNR'?
LVL 1
JDCamAsked:
Who is Participating?
 
Scott PletcherSenior DBACommented:
You must be careful to put the ISNULL outside/before the MAX, not inside it, like so:

select
      REF,
      DATE,
      LEV1,
      LEV2,
      ISNULL(MAX(FROM_CODE), 'UNR') as FROM_CODE,
      SUM(FROM_QTY) as FROM_QTY,
      ISNULL(MAX(TO_CODE), 'UNR') as TO_CODE,
      SUM(TO_QTY) as TO_QTY
      FROM BVF_947
      GROUP BY REF, DATE, LEV1, LEV2

This instead:
      MAX(ISNULL(FROM_CODE, 'UNR')) as FROM_CODE,
could cause bogus results.
0
 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If you're doing MAX and SUM, explain how nulls should behave.  
   Throw them out when calculating MAX?
   Counted or not counted in the SUM?
   If it encounters a single NULL then have the MAX() or SUM() value return 'UNR'?
0
 
JDCamAuthor Commented:
Interesting....
In the current output, the single line produced by the group by could contain a null because the entire column for this record is null. In which case I am looking for 'UNR'.  If there is any value in the column then I want to keep it.

COALESCE(FROM_CODE, 'UNR')  is what I was thinking
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorCommented:
If you forget about the SUM and MAX part, then yes COALESCE and ISNULL would both work.
select
   REF,
   DATE,
   LEV1,
   LEV2,
   COALESCE(FROM_CODE, 'UNR') as FROM_CODE,   -- this works
   COALESCE(FROM_QTY, 'UNR') as FROM_QTY,
   ISNULL(TO_CODE, 'UNR') as TO_CODE,         -- this works too
   ISNULL(TO_QTY, 'UNR') as TO_QTY
FROM BVF_947

Open in new window


>I use the below query to summarize multiple rows into a single row
Not abundantly sure that I'm getting, or answering, your question though.  Explain where 'multiple rows' comes into play.  A data mockup would be good here.

btw the difference between the two is that ISNULL is a 'this or that', with two values, and COALESCE can handle multiple values i.e. COALESE (this, that, someother)
0
 
Vitor MontalvãoMSSQL Senior EngineerCommented:
JDCam, do you still need help on this question?
0
 
JDCamAuthor Commented:
I ended up with this which appears to be working...

select
      REF,
      DATE,
      LEV1,
      LEV2,
      COALESCE(MAX(FROM_CODE),'UNR')as FROM_CODE,
      COALESCE(SUM(FROM_QTY), (SUM(TO_QTY)*-1))as FROM_QTY,
      COALESCE(MAX(TO_CODE),'UNR')as TO_CODE,
      COALESCE(SUM(TO_QTY), (SUM(FROM_QTY)*1)) as TO_QTY      
      FROM BVF_947
      GROUP BY REF, DATE, LEV1, LEV2
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.