Hi Guys
I have been able to get what i want from the requirement i was presented but not sure about the weeknumber as that is the only column which is incorrect. Apart from that every data I have in my result set is correct
declare @stdate as date
declare @eddate as date
set @stdate = '20201122'
set @eddate = '20201128'
select
x.SKU
, x.SKUVariant
, x.RetailPrice
, x.[Description]
, SUM(x.Units) as [Total Units]
, x.Name
, 'WGO' as [OrderType]
, DATEPART(wk,x.Startdate) as WeekNum
,x.Startdate
from
(
select
p.Code as SKU
, pv.Code as SKUVariant
, cpr.GrossPrice as RetailPrice
, h.OrderNumber
, cast(
case
when ob.BatchType = 1 then CompletedOn
when pv.Code = '235689' then i.UpdatedOn
else isnull(ob.ReleaseDate, h.StartDateTime)
end as date) as Startdate
, ISNULL(P.Description, P.Name) as [Description]
, SUM(i.Quantity) as Units
, case dm.pkDeliveryMethodId
when 1 then 'Collect From Store'
when 2 then 'Order into Store'
else 'Delivery'
end as [Name]
from
Header h with (nolock)
inner join ItemLine i with (nolock) on i.fkHeaderId = h.pkHeaderId
inner join ProductVar pv with (nolock) on pv.pkProductVariantID = i.fkProductVariantId
inner join Product p with (nolock) on p.pkProductID = pv.fkProductID
inner join [Order] o with (nolock) on o.OrderNumber = h.OrderNumber
inner join Branch b with (nolock) on b.pkBranchID = h.fkBranchId
left join Branch br with (nolock) on br.pkBranchID = o.OriginalBranchId
left join Order_Line ol with (nolock) on ol.OrderId = o.pkOrderId and ol.LineId = i.LineNumber
left join OrderBatch ob with (nolock) on ob.pkOrderBatchId = ol.OrderBatchId
left join DeliveryMethod dm with (nolock) on dm.pkDeliveryMethodId = o.DeliveryMethodId
left JOIN CompanyPriceList AS CPr WITH (NOLOCK) ON CPr.fkProductID = P.pkProductID
AND GETDATE() BETWEEN CPr.EffectiveFrom AND CPr.EffectiveTo
and cpr.fkProductVariantID is null
and cpr.countryISOcode = 'GBR'
where
br.Code > 500
and (
(
(
isnull(ob.BatchType,'4') <> 1 and cast(isnull(ob.ReleaseDate, h.StartDateTime) as date) between @StDate and @EdDate and pv.Code <> '235689'
) or pv.Code = '235689' and cast(i.UpdatedOn as date) between @StDate and @EdDate
) or ob.BatchType = 1 and cast(CompletedOn as date) between @StDate and @EdDate
)
and h.IsVoided = 0
and i.IsVoided = 0
and b.Code <> 808
and h.fkTypeId in (1,2,3,14,208,209)
and (
(
h.enmStatus in (5,6,7,36,69) and isnull(ob.BatchType,'4') <> 1
) or ob.BatchType = 1 and ob.IsComplete = 1
)
and pv.Code not in ('235689')
group by
p.Code
, pv.Code
, cpr.GrossPrice
, ISNULL(P.Description, P.Name)
, h.OrderNumber
, cast(
case
when ob.BatchType = 1 then CompletedOn
when pv.Code = '235689' then i.UpdatedOn
else isnull(ob.ReleaseDate, h.StartDateTime)
end as date)
, case dm.pkDeliveryMethodId
when 1 then 'Collect From Store'
when 2 then 'Order into Store'
else 'Delivery'
end
) x
group by
x.SKU
, x.SKUVariant
, x.RetailPrice
, x.[Description]
, x.Name
, DATEPART(wk,x.Startdate)
,x.Startdate
union
select
x.SKU
, x.SKUVariant
, x.RetailPrice
, x.[Description]
, SUM(x.Units) as [Total Units]
, x.Name
, 'SGO' as [OrderType]
, DATEPART(wk,x.Startdate) as WeekNum
,x.Startdate
from
(
select
p.Code as SKU
, pv.Code as SKUVariant
, cpr.GrossPrice as RetailPrice
, h.OrderNumber
, cast(
case
when ob.BatchType = 1 then CompletedOn
when pv.Code = '235689' then i.UpdatedOn
else isnull(ob.ReleaseDate, h.StartDateTime)
end as date) as Startdate
, ISNULL(P.Description, P.Name) as [Description]
, SUM(i.Quantity) as Units
, case dm.pkDeliveryMethodId
when 1 then 'Collect From Store'
when 2 then 'Order into Store'
else 'Delivery'
end as [Name]
from
Header h with (nolock)
inner join ItemLine i with (nolock) on i.fkHeaderId = h.pkHeaderId
inner join ProductVariant pv with (nolock) on pv.pkProductVariantID = i.fkProductVariantId
inner join Product p with (nolock) on p.pkProductID = pv.fkProductID
inner join [Order] o with (nolock) on o.OrderNumber = h.OrderNumber
inner join Branch b with (nolock) on b.pkBranchID = h.fkBranchId
left join Branch br with (nolock) on br.pkBranchID = o.OriginalBranchId
left join Order_Line ol with (nolock) on ol.OrderId = o.pkOrderId and ol.LineId = i.LineNumber
left join OrderBatch ob with (nolock) on ob.pkOrderBatchId = ol.OrderBatchId
left join DeliveryMethod dm with (nolock) on dm.pkDeliveryMethodId = o.DeliveryMethodId
left JOIN CompanyPriceList AS CPr WITH (NOLOCK) ON CPr.fkProductID = P.pkProductID
AND GETDATE() BETWEEN CPr.EffectiveFrom AND CPr.EffectiveTo
and cpr.fkProductVariantID is null
and cpr.countryISOcode = 'GBR'
where
br.Code < 500
and (
(
(
isnull(ob.BatchType,'4') <> 1 and cast(isnull(ob.ReleaseDate, h.StartDateTime) as date) between @StDate and @EdDate and pv.Code <> '235689'
) or pv.Code = '235689' and cast(i.UpdatedOn as date) between @StDate and @EdDate
) or ob.BatchType = 1 and cast(CompletedOn as date) between @StDate and @EdDate
)
and h.IsVoided = 0
and i.IsVoided = 0
and h.fkTypeId in (1,2,3,14,208,209)
and (
(
h.enmStatus in (5,6,7,36,69) and isnull(ob.BatchType,'4') <> 1
) or ob.BatchType = 1 and ob.IsComplete = 1
)
and pv.Code not in ('235689')
group by
p.Code
, pv.Code
, cpr.GrossPrice
, ISNULL(P.Description, P.Name)
, h.OrderNumber
, cast(
case
when ob.BatchType = 1 then CompletedOn
when pv.Code = '235689' then i.UpdatedOn
else isnull(ob.ReleaseDate, h.StartDateTime)
end as date)
, case dm.pkDeliveryMethodId
when 1 then 'Collect From Store'
when 2 then 'Order into Store'
else 'Delivery'
end
) x
group by
x.SKU
, x.SKUVariant
, x.RetailPrice
, x.[Description]
, x.Name
, DATEPART(wk,x.Startdate)
,x.Startdate
order by
OrderType
, x.Name
,Startdate
GO
The output i see is
The week number calculation is wrong.
I investigated the matter and took this line out
that is
datepart(week,x.startdate).here i see that the output is 48. it should be week 44 as the date is between 22-11-2020 to 28-11-2020
Any help would be appreciated