Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Nested case statement

Posted on 2016-08-26
3
Medium Priority
?
64 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
[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
  • 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
In this article I will describe the Detach & Attach 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 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…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 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