Solved

Help with Case Statement

Posted on 2009-07-07
4
163 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
convert in derived column 7 31
SQL Help 27 47
sql server concatenate fields 10 36
Need help constructing a conditional update query 16 50
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

825 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