Solved

Question regarding time periods

Posted on 2014-12-09
2
74 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

840 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