Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Need suggestions on dividing two rows within a CASE statement Expand / Collapse
Author
Message
Posted Monday, July 28, 2014 12:12 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, October 24, 2014 11:49 AM
Points: 65, Visits: 413
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
Post #1597013
Posted Monday, July 28, 2014 1:06 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:32 AM
Points: 3,806, Visits: 8,559
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.
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?

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1597037
Posted Monday, July 28, 2014 1:55 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Friday, October 24, 2014 11:49 AM
Points: 65, Visits: 413
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
Post #1597050
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse