Solved

Help with Case Statement

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Access Date Query 28 44
encyps queries mssql 15 38
How to place a condition in a filter criteria in t-sql? 12 60
Updating a table from a temp table 4 29
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

862 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

21 Experts available now in Live!

Get 1:1 Help Now