• frdrckmitchell7 (10/24/2014)


    dbo.BI1_view_Dim_ItemMaster IM = keyitem

    BI1_DW_Dim_CalendarDefinition = keydate

    Here's that query again as a reminder. I need to know which table the column "keyitem" comes from:

    /*

    dbo.BI1_view_Dim_ItemMaster IM = keyitem

    BI1_DW_Dim_CalendarDefinition = keydate

    */

    --------------------------------------------------------------------------------------

    -- PlannedRequirement

    --------------------------------------------------------------------------------------

    IF OBJECT_ID ('tempdb..#PlannedRequirement') IS NOT NULL DROP TABLE #PlannedRequirement

    SELECT KeyDepot, KeyItem

    ,SUM(A.Period1starting20130601) As Period1_starting_20130601

    ,SUM(A.Period1DateDiffTotal) As Period1DateDiffTotal

    ,SUM(A.Period2starting20130701) As Period2_starting_20130701

    ,SUM(A.Period2DateDiffTotal) As Period2DateDiffTotal

    ,SUM(A.Period3starting20130801) As Period3_starting_20130801

    ,SUM(A.Period3DateDiffTotal) As Period3DateDiffTotal

    ,SUM(A.Period4starting20130901) As Period4_starting_20130901

    ,SUM(A.Period4DateDiffTotal) As Period4DateDiffTotal

    ,SUM(A.Period5starting20131001) As Period5_starting_20131001

    ,SUM(A.Period5DateDiffTotal) As Period5DateDiffTotal

    ,SUM(A.Period6starting20131101) As Period6_starting_20131101

    ,SUM(A.Period6DateDiffTotal) As Period6DateDiffTotal

    ,SUM(A.Period7starting20131201) As Period7_starting_20131201

    ,SUM(A.Period7DateDiffTotal) As Period7DateDiffTotal

    ,SUM(A.Period8starting20140101) As Period8_starting_20140101

    ,SUM(A.Period8DateDiffTotal) As Period8DateDiffTotal

    ,SUM(A.Period9starting20150601) As Period9_starting_20150601

    ,SUM(A.Period9DateDiffTotal) As Period9DateDiffTotal

    ,SUM(A.Period10starting20150701) As Period10_starting_20150701

    ,SUM(A.Period10DateDiffTotal) As Period10DateDiffTotal

    ,SUM(A.Period11starting20150801) As Period11_starting_20150801

    ,SUM(A.Period11DateDiffTotal) As Period11DateDiffTotal

    ,SUM(A.Period12starting20150901) As Period12_starting_20150901

    ,SUM(A.Period12DateDiffTotal) As Period12DateDiffTotal

    ,SUM(Total12Periods) As TotalForAll12Periods

    INTO #PlannedRequirement

    FROM ( -- A

    SELECT DISTINCT

    KeyDepotPlannedOrder as KeyDepot,

    KeyItem,

    PlannedRelease.TransDateNumeric,

    PlannedRelease.keydate

    ,CASE WHEN x.DateRange = 1 THEN QuantityPlanned ELSE 0 END As Period1starting20130601

    ,CASE WHEN x.DateRange = 1 AND Condition2 = 1 THEN 1 ELSE 0 END As Period1DateDiffTotal

    ,CASE WHEN x.DateRange = 2 THEN QuantityPlanned ELSE 0 END As Period2starting20130701

    ,CASE WHEN x.DateRange = 2 AND Condition2 = 1 THEN 1 ELSE 0 END As Period2DateDiffTotal

    ,CASE WHEN x.DateRange = 3 THEN QuantityPlanned ELSE 0 END As Period3starting20130801

    ,CASE WHEN x.DateRange = 3 AND Condition2 = 1 THEN 1 ELSE 0 END As Period3DateDiffTotal

    ,CASE WHEN x.DateRange = 4 THEN QuantityPlanned ELSE 0 END As Period4starting20130901

    ,CASE WHEN x.DateRange = 4 AND Condition2 = 1 THEN 1 ELSE 0 END As Period4DateDiffTotal

    ,CASE WHEN x.DateRange = 5 THEN QuantityPlanned ELSE 0 END As Period5starting20131001

    ,CASE WHEN x.DateRange = 5 AND Condition2 = 1 THEN 1 ELSE 0 END As Period5DateDiffTotal

    ,CASE WHEN x.DateRange = 6 THEN QuantityPlanned ELSE 0 END As Period6starting20131101

    ,CASE WHEN x.DateRange = 6 AND Condition2 = 1 THEN 1 ELSE 0 END As Period6DateDiffTotal

    ,CASE WHEN x.DateRange = 7 THEN QuantityPlanned ELSE 0 END As Period7starting20131201

    ,CASE WHEN x.DateRange = 7 AND Condition2 = 1 THEN 1 ELSE 0 END As Period7DateDiffTotal

    ,CASE WHEN x.DateRange = 8 THEN QuantityPlanned ELSE 0 END As Period8starting20140101

    ,CASE WHEN x.DateRange = 8 AND Condition2 = 1 THEN 1 ELSE 0 END As Period8DateDiffTotal

    ,CASE WHEN x.DateRange = 9 THEN QuantityPlanned ELSE 0 END As Period9starting20150601

    ,CASE WHEN x.DateRange = 9 AND Condition2 = 1 THEN 1 ELSE 0 END As Period9DateDiffTotal

    ,CASE WHEN x.DateRange = 10 THEN QuantityPlanned ELSE 0 END As Period10starting20150701

    ,CASE WHEN x.DateRange = 10 AND Condition2 = 1 THEN 1 ELSE 0 END As Period10DateDiffTotal

    ,CASE WHEN x.DateRange = 11 THEN QuantityPlanned ELSE 0 END As Period11starting20150801

    ,CASE WHEN x.DateRange = 11 AND Condition2 = 1 THEN 1 ELSE 0 END As Period11DateDiffTotal

    ,CASE WHEN x.DateRange = 12 THEN QuantityPlanned ELSE 0 END As Period12starting20150901

    ,CASE WHEN x.DateRange = 12 AND Condition2 = 1 THEN 1 ELSE 0 END As Period12DateDiffTotal

    ,CASE

    WHEN 12<>1 THEN CASE WHEN x.DateRange = 99 THEN QuantityPlanned ELSE 0 END

    ELSE CASE WHEN x.DateRange = 1 THEN QuantityPlanned ELSE 0 END

    END As Total12Periods -- CJM this doesn't look correct, it should be x.DateRange = 99, the whole range

    FROM BI1_DW_Fact_FirmPlannedOrders (NOLOCK)

    INNER JOIN dbo.BI1_DW_Dim_DepotMaster DM (NOLOCK)

    ON DM.KeyDepot = KeyDepotPlannedOrder

    LEFT JOIN BI1_DW_Dim_CalendarDefinition PlannedRelease (NOLOCK)

    on PlannedRelease.keydate = KeyDatePlannedRelease -- ##

    LEFT JOIN BI1_DW_Dim_CalendarDefinition PlannedDue (NOLOCK)

    on PlannedDue.keydate = KeyDatePlannedDue -- ##

    LEFT JOIN BI1_DW_Dim_CalendarDefinition RescheduleMRP (NOLOCK)

    on RescheduleMRP.keydate = KeyDateRescheduleMRP -- ##

    CROSS APPLY (

    SELECT DateRange = CASE

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20131001 AND 20131031 THEN 1

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20131101 AND 20131130 THEN 2

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20131201 AND 20131231 THEN 3

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140101 AND 20140131 THEN 4

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140201 AND 20140228 THEN 5

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140301 AND 20140331 THEN 6

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140401 AND 20140430 THEN 7

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140501 AND 20140531 THEN 8

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140601 AND 20140630 THEN 9

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140701 AND 20140731 THEN 10

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140801 AND 20140831 THEN 11

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20140901 AND 20140930 THEN 12

    WHEN PlannedRelease.TransDateNumeric BETWEEN 20131001 AND 20140930 THEN 99

    ELSE NULL END,

    Condition2 = CASE WHEN PlannedDue.TransDateNumeric<>RescheduleMRP.TransDateNumeric AND RescheduleMRP.TransDateNumeric<>0 THEN 1 ELSE 0 END

    ) x

    WHERE KeyCompany = 1 AND BI1_DW_Fact_FirmPlannedOrders.RecordID NOT LIKE '%Z'

    ) A

    GROUP BY KeyDepot, KeyItem

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden