Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. declare @Today as Date = GetDate()
  2. declare @StartOfMonth as Date = DateAdd( day, 1 - Day( @Today ), @Today )
  3. declare @EndOfMonth as Date = DateAdd( day, -1, DateAdd( month, 1, @StartOfMonth ) )
  4. declare @StartOfMonthLastYear as Date = DateAdd ( year, -1, @StartofMonth )
  5. declare @EndOfMonthLastYear as Date = DateAdd ( year, -1, @EndofMonth )
  6. declare @StartOfYear as Date = DateAdd(yy, DateDiff(yy,0,getdate()), 0)
  7. declare @EndOfYear as Date = DateAdd(yy, DateDiff(yy,0,getdate()) + 1, -1)
  8. declare @StartOfLastYear as Date = DateAdd( year, -1, @StartOfYear )
  9. declare @EndOfLastYear as Date = DateAdd ( year, -1, @EndOfYear )
  10.  
  11. select LastYear.SellerName, LastYear.BrandName, LastYear.LastYear, CurrentMonthLastYear.CurrentMonthLastYear, CurrentMonth.CurrentMonth, LastYear.Goal
  12. from
  13.  
  14. (select SellerName, BrandName, sum(Caseequiv) as CurrentMonth
  15. from
  16. (select e.RecName as SellerName
  17. , i.BrandNid as 'Brand'
  18. , b.RecName as BrandName
  19. , sum(ol.QtyDelivered) as 'Qty'
  20. , ((sum(ol.QtyDelivered) *1.0) / i.AltPackCasecount) as Caseequiv
  21.  
  22. from OrderLines as ol
  23. join Orders as o on ol.OrderTicketNumber=o.TicketNumber
  24. join Customers as c on ol.ParentCusNid=c.CusNid
  25. join Employees as e on ol.ParentSlsEmpNid=e.EmpNid
  26. join Items as i on ol.ItemNid=i.ItemNid
  27. join Brands as b on b.BrandNid = i.BrandNid
  28. where PrimarySupNid=39 and o.ShippedDate between @StartofMonth and @EndofMonth
  29. group by e.RecName, i.BrandNid, b.RecName, i.AltPackCasecount
  30. ) x
  31. group by SellerName, BrandName
  32. ) as CurrentMonth
  33.  
  34. right join
  35.  
  36. (
  37. select SellerName, BrandName, sum(Caseequiv) as CurrentMonthLastYear
  38. from
  39. (select e.RecName as SellerName
  40. , i.BrandNid as 'Brand'
  41. , b.RecName as BrandName
  42. , sum(ol.QtyDelivered) as 'Qty'
  43. , ((sum(ol.QtyDelivered) *1.0) / i.AltPackCasecount) as Caseequiv
  44.  
  45. from OrderLines as ol
  46. join Orders as o on ol.OrderTicketNumber=o.TicketNumber
  47. join Customers as c on ol.ParentCusNid=c.CusNid
  48. join Employees as e on ol.ParentSlsEmpNid=e.EmpNid
  49. join Items as i on ol.ItemNid=i.ItemNid
  50. join Brands as b on b.BrandNid = i.BrandNid
  51. where PrimarySupNid=39 and o.ShippedDate between @StartOfMonthLastYear and @EndOfMonthLastYear
  52. group by e.RecName, i.BrandNid, b.RecName, i.AltPackCasecount
  53. ) x
  54. group by SellerName, BrandName
  55. ) as CurrentMonthLastYear
  56.  
  57. on CurrentMonth.SellerName = CurrentMonthLastYear.SellerName and CurrentMonth.BrandName = CurrentMonthLastYear.BrandName
  58.  
  59. right join
  60.  
  61. (
  62. select SellerName, BrandName, sum(Caseequiv) as LastYear, (sum(Caseequiv) / 12) as Goal
  63. from
  64. (select e.RecName as SellerName
  65. , i.BrandNid as 'Brand'
  66. , b.RecName as BrandName
  67. , sum(ol.QtyDelivered) as 'Qty'
  68. , ((sum(ol.QtyDelivered) *1.0) / i.AltPackCasecount) as Caseequiv
  69.  
  70. from OrderLines as ol
  71. join Orders as o on ol.OrderTicketNumber=o.TicketNumber
  72. join Customers as c on ol.ParentCusNid=c.CusNid
  73. join Employees as e on ol.ParentSlsEmpNid=e.EmpNid
  74. join Items as i on ol.ItemNid=i.ItemNid
  75. join Brands as b on b.BrandNid = i.BrandNid
  76. where PrimarySupNid=39 and o.ShippedDate between @StartOfLastYear and @EndOfLastYear
  77. group by e.RecName, i.BrandNid, b.RecName, i.AltPackCasecount
  78. ) x
  79. group by SellerName, BrandName
  80. ) as LastYear
  81.  
  82. on CurrentMonth.SellerName = CurrentMonthLastYear.SellerName and CurrentMonth.BrandName = CurrentMonthLastYear.BrandName and CurrentMonth.SellerName = LastYear.SellerName and CurrentMonth.BrandName = LastYear.BrandName
  83. order by LastYear.SellerName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement