Need suggestions on dividing two rows within a CASE statement

  • 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);

    SELECTDT.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

    SELECTISNULL(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

    ANDBF_DATE_CREATED >= @Start

    ANDBF_DATE_CREATED < @End

    ANDBF_GUID = BF_PARENT_ID

    ANDBF_QUICK_QUOTE = 0

    GROUP BYOC.OfferCode,

    ISNULL(DATEADD(day, -0, DATEDIFF(Day, 0, BF_DATE_CREATED)), '1900-01-01 00:00:00.000'),

    CASEWHEN 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

  • 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
  • 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 AXXX Inventory (A) 84

    2014-07-21 00:00:00.000 AXXX Inventory with Lead 45

    2014-07-21 00:00:00.000AXXX 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

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply