Solved

Nested case statement

Posted on 2016-08-26
3
47 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 69

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 69

Accepted Solution

by:
Qlemo earned 500 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

785 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