Solved

Question regarding time periods

Posted on 2014-12-09
2
70 Views
Last Modified: 2014-12-11
Hi experts,
  Below is my store proc for a bike sale, I am getting the results of individual CTE's correclty when I hardcode the time period values but at the end result I am getting the same result for CTE4, CTE5 and CTE6.
  I have checked if the timeperiods are coming out right and they is no problem with that, please suggest if I am going wrong somewhere in the pivot

                 SELECT
                 M.Added AS DateAdded,
                 M.Customer_No,
                 M.FirstName,
                 M.LastName,
                 M.List_No,
                 MC.Birthyear,
                 M.[State],
                 M.Country,
                 M.Source_Code,
                 replace(M.Sub_Source_Code, ',', '&') AS Sub_Source_Code
                 , M.Added T1, DATEADD(YEAR, 1, DATEADD(DAY, -1, M.Added)) T2, DATEADD(YEAR, 1, M.Added) T3, DATEADD(YEAR, 1, DATEADD(DAY, -1, M.Added)) T4, DATEADD(YEAR, 1, M.Added) T5, DATEADD(YEAR, 1, DATEADD(DAY, -1, M.Added)) T6
                  INTO  #CTESOURCE  FROM He.dbo.MLCC M WITH (NOLOCK)
                            cross apply (
                    SELECT charindex('~', M.Sub_Source_Code, 1) AS pos1
                             ) AS xapply1 (pos1)
                cross apply (
                   SELECT CASE WHEN xapply1.pos1 > 0 THEN left(M.Sub_Source_Code,xapply1.pos1 - 1)
                         ELSE M.Sub_Source_Code
                    END AS consider_code
                            ) AS xapply2 (consider_code)
                WHERE
              M.Customer_No < 50000000
             
             
                         
; WITH cteYearRanges(Label, YearsToAdd, DaysToSubtract) AS
(
SELECT 'T1', 0, 0
UNION ALL
SELECT 'T2', 1, 1
UNION ALL
SELECT 'T3', 1, 0
UNION ALL
SELECT 'T4', 2, 1
UNION ALL
SELECT 'T5', 2, 0
UNION ALL
SELECT 'T6', 3, 1
)
SELECT * INTO #TEMPCUST FROM
(
SELECT  C.Customer_no,
        C.DateAdded,
       cteYearRanges.Label,
        DATEADD(YEAR, cteYearRanges.YearsToAdd, DATEADD(DAY, -cteYearRanges.DaysToSubtract, C.DateAdded)) d
FROM #CTESource C
CROSS JOINcteYearRanges
WHERE DATEADD(YEAR, cteYearRanges.YearsToAdd, DATEADD(DAY, -cteYearRanges.DaysToSubtract, C.DateAdded)) <= GETDATE()
) p

PIVOT (MIN(d) FOR Label IN ([T1], [T2], [T3], [T4], [T5], [T6])) pvt   ;
     
 
   
WITH  CTE4( CustomerNo, LotsPurBikes0910, AucPurBikes09To10) AS(
    SELECT  DISTINCT S.Customer_No, COUNT(S.Lot_No) AS LotsPurBikes0910, SUM(S.PRwithBP) AS AucPurBikes09To10  
    FROM    HI.dbo.AllSales S WITH (NOLOCK)
    LEFT OUTER JOIN    HI.dbo.BikeSale A WITH (NOLOCK)
    ON      S.Sale_No = A.Sale_No
    LEFT OUTER JOIN    #TEMPCUST CS
    ON      CS.Customer_No = S.Customer_No
    AND     A.BikeSale_End_Date >= T1      
    AND     A.BikeSale_End_Date <= T2  
    WHERE   Buy_Back = 0
    AND A.BikeSaleSubTypeID IN (5, 18)
    GROUP BY S.Customer_No
    ),
 
    CTE5( CustomerNo, LotsPurBikes1011, AucPurBikes10To11) AS(  
    SELECT  DISTINCT S.Customer_No, COUNT(S.Lot_No) AS LotsPurBikes1011, SUM(S.PRwithBP) AS AucPurBikes10To11
    FROM    HI.dbo.AllSales S WITH (NOLOCK)
    LEFT OUTER JOIN    HI.dbo.BikeSale A WITH (NOLOCK)
    ON      S.Sale_No = A.Sale_No
    LEFT OUTER JOIN    #TEMPCUST CS
    ON      CS.Customer_No = S.Customer_No
    AND     A.BikeSale_End_Date >= T3      
    AND     A.BikeSale_End_Date <= T4  
    WHERE   Buy_Back = 0
    AND A.BikeSaleSubTypeID IN (5, 18)
    GROUP BY S.Customer_No
    ),
   
    CTE6( CustomerNo, LotsPurBikes1112, AucPurBikes11To12) AS(
    SELECT  DISTINCT S.Customer_No, COUNT(S.Lot_No) AS LotsPurBikes1112, SUM(S.PRwithBP) AS AucPurBikes11To12
    FROM    HI.dbo.AllSales S WITH (NOLOCK)
    LEFT OUTER JOIN    HI.dbo.BikeSale A WITH (NOLOCK)
    ON      S.Sale_No = A.Sale_No
    LEFT OUTER JOIN    #TEMPCUST CS
    ON      CS.Customer_No = S.Customer_No
    AND     A.BikeSale_End_Date >= T5      
    AND     A.BikeSale_End_Date <= T6  
    WHERE   Buy_Back = 0
    AND A.BikeSaleSubTypeID IN (5, 18)
    GROUP BY S.Customer_No
        ),
       
   
  SELECT A.*,L.*     FROM  #CTESOURCE A
                     LEFT OUTER JOIN CTE11 L on L.CustomerNo = A.Customer_No                                                
                               WHERE A.Customer_No = 1363871
0
Comment
Question by:sqlcurious
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 40490452
Have you checked your #tempCust table? Does it have the correct date ranges? Please note that the date ranges will be relative to the DateAdded per each customer
0
 

Author Closing Comment

by:sqlcurious
ID: 40494358
thanks!
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

In this article I will describe the Copy Database Wizard 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.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

759 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now