Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Nested case statement

Posted on 2016-08-26
3
Medium Priority
?
74 Views
Last Modified: 2016-08-26
Here is some sample data from a sql server 2012 table named MailHouse

Campaign ID      Offer1Type       Offer1DollarAmount            Offer2Type     Offer2DollarAmount
             1                 FCP                     10                                         Points               20
             2                Points                 40                                         FCP                   30
             3                 FCP                      50
             4                 FCP                      60                                       Food                  30                
The MailHouse table contains info for 2 different offer types per campaign

as you can from the above data,  a campaign may have 1 or 2 different offers and the offers can be in any order

I am trying to write a query that will look at the offertype  for each campaign and give an fcpdollar, pointsdollar, FoodDollar value

Using the above sample data I would like

CampaignID     FCPDollar   PointDollar      FoodDollar
 1                         10               20                      0  
 2                         30               40                      0
 3                         50                0                       0
4                          60                0                       30

I'm assuming the query when involve nested case statements on offer type but not sure
0
Comment
Question by:johnnyg123
  • 2
3 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 41772104
First, I would normalize the two offers into one per row.
select CampaignID, Offer1Type as OfferType, Offer1DollarAmount as OfferDollarAmount from MailHouse
union all
select CampaignID, Offer2Type, Offer2DollarAmount from MailHouse

Open in new window

Then we can work easily on each type:
select CampaignID,
       FCPDollar   = sum(case OfferType when 'FCP' then OfferDollarAmount end),
       PointDollar = sum(case OfferType when 'Points' then OfferDollarAmount end),
       FoodDollar  = sum(case OfferType when 'Food' then OfferDollarAmount end)
from (
  select CampaignID, Offer1Type as OfferType, Offer1DollarAmount as OfferDollarAmount from MailHouse
  union all
  select CampaignID, Offer2Type, Offer2DollarAmount from MailHouse
) data
group by CampaignID

Open in new window

0
 

Author Comment

by:johnnyg123
ID: 41772154
Thanks so much for the post! That absolutely does work

I was anticipating the answer to involve use of case statement so I didn't list all the fields


There are additional text fields that need to come along as well which can't be summed


CampaignID     FCPDollar   PointDollar      FoodDollar
  1                         10               20                      0  
  2                         30               40                      0
  3                         50                0                       0
 4                          60                0                       30




Also, this will be used inside a larger query


would it be possible to do it using nested case statements?
0
 
LVL 71

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 41772377
You can always group by those extra columns, but it can get cumbersome if there a lot of them.
You can use the result as a CTE or like I used the union (building a temporary table).

A nested or combined case would not change anything, just remove the union.
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

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. …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Loops Section Overview

885 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