I am working on a Report and require some help with the query design. In the query, I have the Forecasted Sales and the Forecasted Inventory ( in monthly buckets). What I need to do is to calculate the inventory coverage (in number of months) in each month. The definition of inventory coverage would be as follows:
If the inventory I have at the end of the month is able to meet the demand ( forecasted sales) for the next X months, then the Inventory Coverage that month is X.
with cte1
as (select a.[period]
,case
when a.inventory
>sum(b.netsales) over(partition by a.[period]
order by b.[period]
rows unbounded preceding
)
then 1.
else (a.inventory
-sum(b.netsales) over(partition by a.[period]
order by b.[period]
rows unbounded preceding
)
+b.netsales
)
/b.netsales
end as coverage
,row_number() over(partition by a.[period]
order by b.[period]
)
as rn
from dbo.test_table as a
left outer join dbo.test_table as b
on b.[period]>a.[period]
)
,cte2
as (select a.[period]
,sum(b.coverage) as coverage
from (select [period]
,max(rn)+1-case when sum(coverage) is null then 1 else 0 end as rn
from cte1
where isnull(coverage,0)>=1
or rn=1
group by [period]
) as a
inner join cte1 as b
on b.[period]=a.[period]
and b.rn<=a.rn
group by a.[period]
)
select p.txt as [ ]
,p.[201701] as Month1
,p.[201702] as Month2
,p.[201703] as Month3
,p.[201704] as Month4
,p.[201705] as Month5
from (select 'Net Sales' as txt
,[period]
,trim(str(netsales)) as thevalue
,1 as seq
from dbo.test_table
union all
select 'Inventiry' as txt
,[period]
,trim(str(inventory)) as thevalue
,2 as seq
from dbo.test_table
union all
select 'Coverage' as txt
,[period]
,case when coverage is null then '-' else trim(str(coverage,10,2)) end as thevalue
,3 as seq
from cte2
) as a
pivot (min(a.thevalue) for a.[period] in ([201701],[201702],[201703],[201704],[201705])) as p
order by p.seq
;