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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Acronis Data Cloud 7.8 Enhances Cyber Protection

A closer look at five essential enhancements that benefit end-users and help MSPs take their cloud data protection business further.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.