Solved

Question regarding time periods

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

815 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

8 Experts available now in Live!

Get 1:1 Help Now