Oracle query - Case statement

Select 
           name,	
			NVL(SUM(CASE WHEN vp.inv_product_type = 'RBC' THEN id.order_qty ELSE 0 END ), 0),-- 'RBC',	
			NVL(SUM(CASE WHEN vp.inv_product_type = 'LRBC' THEN id.order_qty ELSE 0 END), 0),-- 'LRBC',		
			NVL(SUM(CASE WHEN vp.inv_product_type in ('PHER','VPHER', 'LPHER') THEN id.order_qty ELSE 0 END), 0),-- 'PHERESIS',		
			NVL(SUM(CASE WHEN vp.inv_product_type in ('FP24','FFP') THEN id.order_qty ELSE 0 END), 0),-- 'FFPUTIL',	
			NVL(SUM(CASE WHEN vp.inv_product_type = 'CPPLS' THEN id.order_qty ELSE 0 END), 0),-- 'CPPUTIL',	
			NVL(SUM(CASE WHEN vp.inv_product_type = 'CRYP' THEN id.order_qty ELSE 0 END), 0),-- 'CRYO',	
			NVL(SUM(CASE WHEN vp.inv_product_type = 'POCRY' THEN id.order_qty ELSE 0 END), 0)-- 'POCRY'      
from    invoice_header ih,
        invoice_detail id,
		item_profile ip,
		customers@plab.world c,
		valid_products@plab.world vp
where id.invoice_number = ih.invoice_number
and   id.item_id = ip.item_id
and   ip.item_type in ('P')
and   ih.customer_id = c.customer_id
and   trunc(ih.invoice_date) between  '01-jun-2015' and '30-jun-2015'
and id.item_id = vp.product_code
and ih.customer_id = 'WAD-RG0005'
group by name,vp.inv_product_type

Open in new window


Required Format

Name                                                                               RBC   LRBC     PHERESIS     FFPUTIL    CPPUTIL    CRYO   POCRY    

ST. ANTHONY-CROWN POINT FRANCISCAN HEALTH    138       0             26                29              0             0              1

My case statement gives an error when I say RBC after END. I have commented it. The above query also gives 5 rows of data instead of one line data. Help appreciated
LVL 6
anumosesAsked:
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.

johnsoneSenior Oracle DBACommented:
The column alias cannot inside the SUM.  It needs to be at the end like this:

NVL(SUM(CASE WHEN vp.inv_product_type = 'RBC' THEN id.order_qty ELSE 0 END ), 0) RBC,

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
anumosesAuthor Commented:
Now column headers are fixed. Get 5 lines instead of one.data
johnsoneSenior Oracle DBACommented:
I believe your group by is incorrect.  You should only be grouping by NAME.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

anumosesAuthor Commented:
Thanks
johnsoneSenior Oracle DBACommented:
Why the B grade?  What information was lacking in the answers?

See grading guidelines -> http://support.experts-exchange.com/customer/portal/articles/481419-what-grade-should-i-award-
anumosesAuthor Commented:
Sorry that was by mistake. Not intentional. If the moderator opens I can grade as A.
anumosesAuthor Commented:
Thanks
johnsoneSenior Oracle DBACommented:
Thanks.
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
Oracle Database

From novice to tech pro — start learning today.