SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need suggestions on dividing two rows within a CASE statement


Need suggestions on dividing two rows within a CASE statement

Author
Message
PFlorenzano-641896
PFlorenzano-641896
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 574
Hello,

I was given the task to come up with a result set based on certain criteria:

Please add one row for each offer code

1) Opt-in rate by offer code: This can be calculated by dividing XXX Inventory with lead / XXX Inventory (A)

The report should read something like below:

Example result set:

Log Date: OfferLetter OfferCode DailyCount

2014-07-20 A XXX Inventory (A) 108
2014-07-20 A XXX Inventory with lead 54
2014-07-20 A XXX Inventory Opt-in Rate: 50%

There are 12 different groupings and OfferLetter A is just one of them.


Below is the code that is written to date:

DECLARE @Start datetime = DATEADD(day, DATEDIFF(day, 0, GETDATE() - 8), 0)
DECLARE @End datetime = DATEADD(day, DATEDIFF(day, 0, GETDATE() - 1) + 1, 0)
DECLARE @Today datetime = DATEADD(day, DATEDIFF(day, 0, GETDATE() - 1), 0);


SELECT DT.OfferCode, DT.OfferCodeDesc
INTO #tempOfferCodes
FROM (
SELECT '' AS 'OfferCode', '' AS 'OfferCodeDesc'
UNION
SELECT 'A', 'ATC Inventory (A)'
UNION
SELECT 'B', 'KBB (B)'
UNION
SELECT 'I', 'iPad Kiosk (I)'
UNION
SELECT 'K', 'Kiosk (K)'
UNION
SELECT 'KBI', 'KBB Inventory (KBI)'
UNION
SELECT 'N', 'KBB (N)'
UNION
SELECT 'VRF', 'Vehicle Range Finder (VRF)'
UNION
SELECT 'W', 'Dealer Website (W)'
UNION
SELECT 'CMB', 'Consumer Unassigned Mobile (CMB)'
UNION
SELECT 'DMB', 'Dealer Mobile (DMB)'
) DT

--Offer Count
SELECT ISNULL(DATEADD(day, -0, DATEDIFF(Day, 0, BF_DATE_CREATED)), '1900-01-01 00:00:00.000') 'LogDate',
OC.OfferCode 'BF_OFFER_CODE',
CASE WHEN OC.OfferCode = '' AND BF_USER_ID = '00000000-0000-0000-0000-000000000000' THEN 'Consumer (Blank)'
WHEN OC.OfferCode = '' AND BF_USER_ID != '00000000-0000-0000-0000-000000000000' THEN 'Dealer (Blank)'
WHEN OC.OfferCode = 'A' AND BF_HAS_LEAD=1 THEN 'ATC Inventory with Lead'
WHEN OC.OfferCode = 'B' AND BF_HAS_LEAD=1 THEN 'KBB with Lead'
WHEN OC.OfferCode = 'I' AND BF_HAS_LEAD=1 THEN 'iPad Kiosk (I) with Lead'
WHEN OC.OfferCode = 'K' AND BF_HAS_LEAD=1 THEN 'Kiosk (K) with Lead'
WHEN OC.OfferCode = 'KBI' AND BF_HAS_LEAD=1 THEN 'KBB Inventory (KBI) with Lead'
WHEN OC.OfferCode = 'N' AND BF_HAS_LEAD=1 THEN 'KBB (N) with Lead'
WHEN OC.OfferCode = 'VRF' AND BF_HAS_LEAD=1 THEN 'Vehicle Range Finder (VRF) with Lead'
WHEN OC.OfferCode = 'W' AND BF_HAS_LEAD=1 THEN 'Dealer Website (W) with Lead'
WHEN OC.OfferCode = 'CMB' AND BF_HAS_LEAD=1 THEN 'Consumer Unassigned Mobile (CMB) with Lead'
WHEN OC.OfferCode = 'DMB' AND BF_HAS_LEAD=1 THEN 'Dealer Mobile (DMB) with Lead'
WHEN OC.OfferCode = 'O' AND BF_HAS_LEAD=1 THEN 'ATC Inventory Opt-in Rate with Lead'
ELSE OC.OfferCodeDesc
END AS 'OfferCode',
SUM(CASE WHEN BF_OFFER_CODE IS NOT NULL THEN 1 ELSE 0 END) 'DailyCount'
FROM #tempOfferCodes OC
LEFT JOIN [bbt_marketplace_main].[dbo].[TIM_PurchaseOffer] PO ON PO.BF_OFFER_CODE = OC.OfferCode
AND BF_DATE_CREATED >= @Start
AND BF_DATE_CREATED < @End
AND BF_GUID = BF_PARENT_ID
AND BF_QUICK_QUOTE = 0

GROUP BY OC.OfferCode,
ISNULL(DATEADD(day, -0, DATEDIFF(Day, 0, BF_DATE_CREATED)), '1900-01-01 00:00:00.000'),
CASE WHEN OC.OfferCode = '' AND BF_USER_ID = '00000000-0000-0000-0000-000000000000' THEN 'Consumer (Blank)'
WHEN OC.OfferCode = '' AND BF_USER_ID != '00000000-0000-0000-0000-000000000000' THEN 'Dealer (Blank)'
WHEN OC.OfferCode = 'A' AND BF_HAS_LEAD=1 THEN 'ATC Inventory with Lead'
WHEN OC.OfferCode = 'B' AND BF_HAS_LEAD=1 THEN 'KBB with Lead'
WHEN OC.OfferCode = 'I' AND BF_HAS_LEAD=1 THEN 'iPad Kiosk (I) with Lead'
WHEN OC.OfferCode = 'K' AND BF_HAS_LEAD=1 THEN 'Kiosk (K) with Lead'
WHEN OC.OfferCode = 'KBI' AND BF_HAS_LEAD=1 THEN 'KBB Inventory (KBI) with Lead'
WHEN OC.OfferCode = 'N' AND BF_HAS_LEAD=1 THEN 'KBB (N) with Lead'
WHEN OC.OfferCode = 'VRF' AND BF_HAS_LEAD=1 THEN 'Vehicle Range Finder (VRF) with Lead'
WHEN OC.OfferCode = 'W' AND BF_HAS_LEAD=1 THEN 'Dealer Website (W) with Lead'
WHEN OC.OfferCode = 'CMB' AND BF_HAS_LEAD=1 THEN 'Consumer Unassigned Mobile (CMB) with Lead'
WHEN OC.OfferCode = 'DMB' AND BF_HAS_LEAD=1 THEN 'Dealer Mobile (DMB) with Lead'
WHEN OC.OfferCode = 'O' AND BF_HAS_LEAD=1 THEN 'ATC Inventory Opt-in Rate with Lead'
ELSE OC.OfferCodeDesc
END

ORDER BY 'LogDate', BF_OFFER_CODE

DROP TABLE #tempOfferCodes


The issue I'm having is that the values I need to divide by are in fact, a result set from the CASE statement. It's been a long time since I've done anything like this.

Any help would be much appreciated.

Thanks
Luis Cazares
Luis Cazares
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16204 Visits: 19059
I don't understand. You say you need to divide the result of a CASE statement, but the result is a string. What are your expected results? A CTE or a subquery could help you to work with the CASE statement as a simple column.
If more help is needed, please post DDL and sample data for [TIM_PurchaseOffer] table.


Luis C.
General Disclaimer:
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?


How to post data/code on a forum to get the best help: Option 1 / Option 2
PFlorenzano-641896
PFlorenzano-641896
SSC-Enthusiastic
SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)SSC-Enthusiastic (138 reputation)

Group: General Forum Members
Points: 138 Visits: 574
Hello Luis,

I need to divide using the DailyCount filed which is in the example below:

SUM(CASE WHEN BF_OFFER_CODE IS NOT NULL THEN 1 ELSE 0 END) 'DailyCount'

2014-07-21 00:00:00.000 A XXX Inventory (A) 84
2014-07-21 00:00:00.000 A XXX Inventory with Lead 45
2014-07-21 00:00:00.000 A XXX Inventory Opt-In Rate 34%

Both the 84 and 45 numbers are derived from the above CASE statement.

What I need to do is create a new row named XXX Inventory Opt-In Rate (row in boldface) and divide the 45 number, which is the XXX Inventory with Lead number by the total grouping for XXX with is 129 (84+45)

Thanks
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search