0

This is my join this is where the issue is i know this to be true

 SELECT DR.DivNo
        ,p.[ProductCode]
        ,p.ProductClass
        ,p.EmpNo
        ,[Description]
        ,[CGNo]
        ,[SCGNo]
        ,dr.Retail
        ,bd.[Buying Director]
  INTO #Product
  FROM .[PRODUCT] p
  LEFT JOIN [DIVRETAIL] DR ON p.ProductCode = DR.ProductCode
  LEFT JOIN vwAllBuyingDirectors bd ON p.EmpNo = bd.bd_BuyingDirector
  WHERE dr.ValidTo IS NULL
        AND dr.DivNo NOT LIKE '8__'
  GROUP BY DR.DivNo
        ,p.[ProductCode]
        ,p.ProductClass
        ,p.EmpNo
        ,bd.[Buying Director]
        ,[Description]
        ,[CGNo]
        ,[SCGNo]
        ,dr.Retail

        --select * from #Product

  SELECT  [Datetime]
        ,[FirstDayOfWeek]
        ,[FirstDayOfMonth]
        ,c.YearWeek
        ,s.NoOfStores
        ,s.DivNo
  INTO #stores
  FROM [Calendar] c
  LEFT JOIN Stores s ON c.FirstDayOfMonth = s.Validfrom
  WHERE c.[Datetime] = firstdayofweek

      --- GIVE STORE COUNT TO EACH DATETIME
  SELECT s.DivNo
        ,c.[Datetime]
        ,c.WeekofYear
        ,c.FirstdayofWeek
        ,c.YearWeek
        ,s.NoOfStores
  INTO #weeklystores
  FROM Calendar c
  LEFT JOIN #stores s ON c.FirstDayOfWeek = s.FirstDayOfWeek
  -- Join numbers from #stores according to first day of week.  Each week onmly has the first day of the week's store count as a reference.

------------------------------------------PREVIOUS WEEK---------------------    --------------------
 SELECT  [Datetime]
        ,[FirstDayOflastWeek]
        ,[FirstDayOflastMonth]
        ,c.YearWeek
        ,s.NoOfStores
        ,s.DivNo
  INTO #storesv2
  FROM [Calendar] c
  LEFT JOIN cbis799p.Portfolio.Stores s ON c.FirstDayOfMonth = s.Validfrom
  WHERE c.[Datetime] = FirstDayOfWeek

  --- GIVE STORE COUNT TO EACH DATETIME
  SELECT s.DivNo
        ,c.[Datetime]
        ,c.WeekofYear
        ,c.FirstdayoflastWeek
        ,c.YearWeek
        ,s.NoOfStores
  INTO #preweeklystores
  FROM Calendar c
  LEFT JOIN #storesv2 s ON c.FirstDayOfWeek = s.FirstDayOfLastWeek  

  /*
 ASSIGN STORE COUNTS AND WEEK NUMBERS TO POS DATA
*/
      CREATE TABLE #salescore (
        [DivNo] NVARCHAR(max)
        ,[ProductCode] INT
        ,[Description] NVARCHAR(max)
        ,[CGNo] NVARCHAR(max)
        ,[SCGNo] NVARCHAR(max)
        ,Retail DECIMAL(38, 2)
        ,[WeekOfYear] TINYINT
        ,FirstDayOfWeek DATETIME
        ,[Quantity] INT
        ,[Sales] DECIMAL(38, 2)
        ,[NoOfStores] INT
        ,[USW] DECIMAL(38, 0)
        )

  INSERT INTO #salescore
  SELECT pos.DivNo
        ,pos.ProductCode
        ,[Description]
        ,[CGNo]
        ,[SCGNo]
        ,p.Retail
        ,w.WeekOfYear
        ,w.FirstDayOfWeek
        ,Sum(Quantity) [Quantity]
        ,sum(pos.Retail) [Retail2]
        ,w.NoOfStores
        ,(Sum(Quantity) / w.NoOfStores) USW
  FROM .pos pos
  LEFT JOIN #weeklystores w ON pos.PosDate = w.[DATETIME]
  LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
        AND p.DivNo = pos.DivNo
        AND pos.DivNo = w.DivNo
  WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
  and w.YearWeek >2013
        and w.YearWeek IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@YEAR, ',')
              )
        AND p.EmpNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@BD, ',')
              )
        AND p.CGNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@CGNo, ',')
              )
        AND p.SCGNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@SCGNo, ',')
              )
        AND p.ProductClass IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@ProductClass, ',')
              )
        AND p.ProductCode IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@ProductCode, ',')
              )
        AND pos.DivNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@Region, ',')
              )
        AND w.WeekOfYear IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@WOY, ',')
              )
        AND w.WeekOfYear IS NOT NULL /* Get rid of pos before goldthorp opening day */
  GROUP BY pos.DivNo
        ,pos.ProductCode
        ,w.WeekOfYear
        ,w.NoOfStores
        ,w.FirstDayOfWeek
        ,[Description]
        ,[CGNo]
        ,[SCGNo]
        ,p.Retail
        ---------------------------------------------

              CREATE TABLE #presales (
        [DivNo] NVARCHAR(max)
        ,[ProductCode] INT
        ,[Description] NVARCHAR(max)
        ,[CGNo] NVARCHAR(max)
        ,[SCGNo] NVARCHAR(max)
        ,Retail DECIMAL(38, 2)
        ,PrevWOY TINYINT
        ,FirstDayOfLastWeek DATETIME
        ,[PreQuantity] INT
        ,[Sales] DECIMAL(38, 2)
        ,[NoOfStores] INT
        ,[USW] DECIMAL(38, 0)
        )

  INSERT INTO #presales
  SELECT pos.DivNo
        ,pos.ProductCode
        ,[Description]
        ,[CGNo]
        ,[SCGNo]
        ,p.Retail
        ,w.WeekOfYear as PrevWOY
        ,w.FirstDayOfLastWeek
        ,SUM(Quantity) Quantity
        ,sum(pos.Retail) [Retail3]
        ,w.NoOfStores
        ,(Sum(Quantity) / w.NoOfStores) USW
  FROM pos pos
  LEFT JOIN #preweeklystores w ON pos.PosDate = w.[DATETIME]
  LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
        AND p.DivNo = pos.DivNo
        AND pos.DivNo = w.DivNo
  WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
  and w.YearWeek >2013
        and w.YearWeek IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@YEAR, ',')
              )
        AND p.EmpNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@BD, ',')
              )
        AND p.CGNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@CGNo, ',')
              )
        AND p.SCGNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@SCGNo, ',')
              )
        AND p.ProductClass IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@ProductClass, ',')
              )
        AND p.ProductCode IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@ProductCode, ',')
              )
        AND pos.DivNo IN (
              SELECT Item
              FROM DataWarehouse.dbo.ufnSplit(@Region, ',')
              )
        --AND w.WeekOfYear IN (
        --    SELECT Item
        --    FROM DataWarehouse.dbo.ufnSplit(@WOY, ',')
        --    )
        AND w.WeekOfYear IS NOT NULL /* Get rid of pos before goldthorp opening day */
  GROUP BY pos.DivNo
        ,pos.ProductCode
        ,w.WeekOfYear
        ,w.NoOfStores
        ,w.FirstDayOfLastWeek
        ,[Description]
        ,[CGNo]
        ,[SCGNo]
        ,p.Retail



        CREATE TABLE #sales (
        [DivNo] NVARCHAR(max)
        ,[ProductCode] INT
        ,[Description] NVARCHAR(max)
        ,[CGNo] NVARCHAR(max)
        ,[SCGNo] NVARCHAR(max)
        ,Retail DECIMAL(38, 2)
        ,[WeekOfYear] TINYINT
        ,FirstDayOfLastWeek DATETIME
        ,[Quantity] int    
        ,[preQuantity] INT
        ,[Sales] DECIMAL(38, 2)
        ,[NoOfStores] INT
        ,[USW] DECIMAL(38, 0)
        ) 
        INSERT INTO #sales
        SELECT pos.DivNo
        ,pos.ProductCode
        ,p.Description
        ,p.CGNo
        ,p.SCGNo
        ,p.Retail
        ,pos.WeekOfYear
        ,ps.FirstDayOfLastWeek
        ,SUM(ps.PreQuantity)---- Quantity 
        ,Sum(pos.Quantity) [Quantity]---- PreQuantity
        ,sum(PS.Sales) Sales
        ,pos.NoOfStores
        ,(Sum(pos.Quantity) / pos.NoOfStores) USW
  FROM #salescore pos
       LEFT JOIN #presales PS 
         on pos.ProductCode = ps.ProductCode 
        and pos.DivNo= ps.DivNo 
        AND pos.FirstDayOfWeek = ps.FirstDayOfLastWeek < --- Issue


  LEFT JOIN #Product p ON p.ProductCode = pos.ProductCode
        AND p.DivNo = pos.DivNo
  WHERE pos.DivNo NOT LIKE '8__' /* Get rid of IRL Regions */
  Group By
        pos.DivNo
        ,pos.ProductCode
        ,p.Description
        ,p.CGNo
        ,p.SCGNo
        ,p.Retail
        ,pos.WeekOfYear
        ,ps.FirstDayOfLastWeek
        ,pos.NoOfStores

this is the output

enter image description here

What i want is

enter image description here

the issue is the first day of week against first day of last week, how can i resolve this basically i need the dates to match up Properly is that makes sence , its hard to explain the script is the same through out just certain data was put into certain tables so that everything could be gathered now i just need to alter my join so it all marry's up

  • Your example output isn't helpful without the source data that is supposed to produce it – Andomar Aug 24 '15 at 09:47
  • @Andomar you want my script or the full output – James Chaggar Aug 24 '15 at 09:48
  • Best is if you can set up an example at http://sqlfiddle.com/ – Andomar Aug 24 '15 at 09:53
  • Screenshots are not super useful, if I wanted to use them I'd have to type everything in again. Your update misses the source data in the tables `#salescore` and `#presales`, without which it is hard to tell what the problem is. – Andomar Aug 24 '15 at 09:55
  • Can you give us an example of your data-set?? Its not clear what the question is and what you are looking for.. – d_luffy_de Aug 24 '15 at 09:57
  • @Andomar ive done this how do i share it – James Chaggar Aug 24 '15 at 09:57
  • One of the buttons on sqlfiddle is "link", this will put an URL in the browser's address bar that you can copy/paste here – Andomar Aug 24 '15 at 09:58
  • @Andomar http://sqlfiddle.com/#!9/d5bcc/1/0 – James Chaggar Aug 24 '15 at 10:00
  • FirstDayOfWeek this column is not available in the dataset... – d_luffy_de Aug 24 '15 at 10:05
  • The result you want Shows in the Quantity-Column the same values as in "PrevQuantity" as shown in the first resultset, right? – CPMunich Aug 24 '15 at 10:10
  • bear with me please @DarshanBI-DW – James Chaggar Aug 24 '15 at 10:10
  • @CPMunich if you look at the screen shots i technically want the quantity to have one more day so that the 1st week of year = 1 but if you look at the dates on sql fidle http://sqlfiddle.com/#!9/1bbc49/1/0 – James Chaggar Aug 24 '15 at 10:18
  • first day of week is the starting date of a week – James Chaggar Aug 24 '15 at 10:18
  • so the week of year = 1 and year = 2015 but the firstdayofweek = 2014-12-29 band finishes in 2015 but for the previous quantity the first day of last week starts in = 2014-12-22 and finishes in 2014 – James Chaggar Aug 24 '15 at 10:20
  • firstdayofweek (1234) Firstdayoflastweek (1231) firstdayofweek (4321) Firstdayoflastweek (1234) firstdayofweek (5678) Firstdayoflastweek (4321) firstdayofweek (7891) Firstdayoflastweek (5678) firstdayofweek (9851) Firstdayoflastweek (7891) is how i want the data – James Chaggar Aug 24 '15 at 10:37
  • I've looked at the fiddle and comments, but I have a hard time figuring out what you're looking for. Could you try to create a simple example with say 5 rows, leaving out irrelevant columns? Provide an explanation for the output. In your current example, I have no idea 3612 comes from. – Andomar Aug 24 '15 at 12:33
  • @Andomar heres what i need to do Then this isn’t a good join… You are telling this week to display last week’s data presumably. You would need to join firstdayoflastweek on firstdayoflastweek and display last week’s data as standalone columns, then work out this week’s separately. – James Chaggar Aug 24 '15 at 13:28
  • @Andomar im saying in here this week to display last week’s data presumably FROM #salescore pos LEFT JOIN #presales PS on pos.ProductCode = ps.ProductCode and pos.DivNo= ps.DivNo AND pos.FirstDayOfWeek = ps.FirstDayOfLastWeek – James Chaggar Aug 24 '15 at 13:29
  • what i need is @Andomar i need to join firstdayoflastweek on firstdayoflastweek and display last week’s data as standalone columns, then work out this week’s separately – James Chaggar Aug 24 '15 at 13:30
  • This question is pretty chaotic now. Perhaps close this question and create a new question that's clearer. Only columns that matter, small rowset, example output, explanation of example output. – Andomar Aug 24 '15 at 13:46

0 Answers0