Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- declare @Today as Date = GetDate()
- declare @StartOfMonth as Date = DateAdd( day, 1 - Day( @Today ), @Today )
- declare @EndOfMonth as Date = DateAdd( day, -1, DateAdd( month, 1, @StartOfMonth ) )
- declare @StartOfMonthLastYear as Date = DateAdd ( year, -1, @StartofMonth )
- declare @EndOfMonthLastYear as Date = DateAdd ( year, -1, @EndofMonth )
- declare @StartOfYear as Date = DateAdd(yy, DateDiff(yy,0,getdate()), 0)
- declare @EndOfYear as Date = DateAdd(yy, DateDiff(yy,0,getdate()) + 1, -1)
- declare @StartOfLastYear as Date = DateAdd( year, -1, @StartOfYear )
- declare @EndOfLastYear as Date = DateAdd ( year, -1, @EndOfYear )
- select LastYear.SellerName, LastYear.BrandName, LastYear.LastYear, CurrentMonthLastYear.CurrentMonthLastYear, CurrentMonth.CurrentMonth, LastYear.Goal
- from
- (select SellerName, BrandName, sum(Caseequiv) as CurrentMonth
- from
- (select e.RecName as SellerName
- , i.BrandNid as 'Brand'
- , b.RecName as BrandName
- , sum(ol.QtyDelivered) as 'Qty'
- , ((sum(ol.QtyDelivered) *1.0) / i.AltPackCasecount) as Caseequiv
- from OrderLines as ol
- join Orders as o on ol.OrderTicketNumber=o.TicketNumber
- join Customers as c on ol.ParentCusNid=c.CusNid
- join Employees as e on ol.ParentSlsEmpNid=e.EmpNid
- join Items as i on ol.ItemNid=i.ItemNid
- join Brands as b on b.BrandNid = i.BrandNid
- where PrimarySupNid=39 and o.ShippedDate between @StartofMonth and @EndofMonth
- group by e.RecName, i.BrandNid, b.RecName, i.AltPackCasecount
- ) x
- group by SellerName, BrandName
- ) as CurrentMonth
- right join
- (
- select SellerName, BrandName, sum(Caseequiv) as CurrentMonthLastYear
- from
- (select e.RecName as SellerName
- , i.BrandNid as 'Brand'
- , b.RecName as BrandName
- , sum(ol.QtyDelivered) as 'Qty'
- , ((sum(ol.QtyDelivered) *1.0) / i.AltPackCasecount) as Caseequiv
- from OrderLines as ol
- join Orders as o on ol.OrderTicketNumber=o.TicketNumber
- join Customers as c on ol.ParentCusNid=c.CusNid
- join Employees as e on ol.ParentSlsEmpNid=e.EmpNid
- join Items as i on ol.ItemNid=i.ItemNid
- join Brands as b on b.BrandNid = i.BrandNid
- where PrimarySupNid=39 and o.ShippedDate between @StartOfMonthLastYear and @EndOfMonthLastYear
- group by e.RecName, i.BrandNid, b.RecName, i.AltPackCasecount
- ) x
- group by SellerName, BrandName
- ) as CurrentMonthLastYear
- on CurrentMonth.SellerName = CurrentMonthLastYear.SellerName and CurrentMonth.BrandName = CurrentMonthLastYear.BrandName
- right join
- (
- select SellerName, BrandName, sum(Caseequiv) as LastYear, (sum(Caseequiv) / 12) as Goal
- from
- (select e.RecName as SellerName
- , i.BrandNid as 'Brand'
- , b.RecName as BrandName
- , sum(ol.QtyDelivered) as 'Qty'
- , ((sum(ol.QtyDelivered) *1.0) / i.AltPackCasecount) as Caseequiv
- from OrderLines as ol
- join Orders as o on ol.OrderTicketNumber=o.TicketNumber
- join Customers as c on ol.ParentCusNid=c.CusNid
- join Employees as e on ol.ParentSlsEmpNid=e.EmpNid
- join Items as i on ol.ItemNid=i.ItemNid
- join Brands as b on b.BrandNid = i.BrandNid
- where PrimarySupNid=39 and o.ShippedDate between @StartOfLastYear and @EndOfLastYear
- group by e.RecName, i.BrandNid, b.RecName, i.AltPackCasecount
- ) x
- group by SellerName, BrandName
- ) as LastYear
- on CurrentMonth.SellerName = CurrentMonthLastYear.SellerName and CurrentMonth.BrandName = CurrentMonthLastYear.BrandName and CurrentMonth.SellerName = LastYear.SellerName and CurrentMonth.BrandName = LastYear.BrandName
- order by LastYear.SellerName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement