Solved

Help with Case Statement

Posted on 2009-07-07
4
169 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 60

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Data architecture is an important aspect in Software as a Service (SaaS) delivery model. This article is a study on the database of a single-tenant application that could be extended to support multiple tenants. The application is web-based develope…
In this article I will describe the Copy Database Wizard 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.
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

632 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