Solved

Help with Case Statement

Posted on 2009-07-07
4
161 Views
Last Modified: 2012-05-07
I only want to return 1 row per "Owner_Name". Right now my statement is displaying NULLs for each Report_GroupID that is present in the database;

select  owner_name as 'Name',
           case when report_groupid = 5 then sum(qty) end as 'RG 5',
           case when report_groupid = 1 or report_groupid = 2 then sum(qty) end as 'RG 1 & 2'
from check_item_record
where storeid = 14
and check_file_date = '7/6/2009'
group by owner_name, report_groupid,  qty

Here is how it is displaying:

Name            RG 5         RG 1 & 2
User1            15            Null
User1            Null         19

I want it to display like this:
Name            RG 5         RG 1 & 2
User1            15            19

Any ideas here?
0
Comment
Question by:mattkovo
4 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 24795818
Try like this:
+Move aggregate outside of case statement.
+Only group by owner_name.
(note you can use in for multiple values of same column)
select  owner_name as 'Name',

sum(case when report_groupid = 5 then qty end) as 'RG 5',

sum(case when report_groupid in (1,2) then qty end) as 'RG 1 & 2'

from check_item_record

where storeid = 14

and check_file_date = '7/6/2009'

group by owner_name

Open in new window

0
 
LVL 75

Expert Comment

by:Aneesh Retnakaran
ID: 24795822
SELECT Owner_name , sum(qty1) as 'RG 5', SUM(qty2) as 'RG 1 & 2'
FROM (
select  owner_name as 'Name',
           case when report_groupid = 5 then qty ELSE 0 end qty1 ,
           case when report_groupid = 1 or report_groupid = 2 then qty else 0 end as qty2  
from check_item_record
where storeid = 14
and check_file_date = '7/6/2009' )
A
group by owner_name
0
 

Author Closing Comment

by:mattkovo
ID: 31600675
That is perfect!  Thanks!
0
 
LVL 17

Expert Comment

by:pssandhu
ID: 24795857
How about:

Select  owner_name as 'Name',

        sum(case when report_groupid = 5 then qty Else 0 end) as 'RG 5',

        sum(case when report_groupid in (1,2) then qty Else 0 end) as 'RG 1 & 2'

from    check_item_record

where   storeid = 14

        and check_file_date = '7/6/2009'

group by owner_name

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now