Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- USE [CNHREMAN_App]
- GO
- /****** Object: StoredProcedure [dbo].[CNH_MaterialsRequirementReport] Script Date: 7/11/2019 4:07:17 PM ******/
- SET ANSI_NULLS ON
- GO
- SET QUOTED_IDENTIFIER ON
- GO
- ALTER PROCEDURE [dbo].[CNH_MaterialsRequirementReport]
- as
- Begin
- If object_ID ('CNH_MaterialsRequirementReportTable') is not null
- BEGIN
- drop table CNH_MaterialsRequirementReportTable
- END
- Create Table CNH_MaterialsRequirementReportTable
- (
- [CNHR Part] varchar (30),
- [Vendor Part#] varchar (30),
- [Description] varchar (300),
- [PC] int,
- [PC Description] varchar (300),
- [FPT] varchar (10),
- [Top 80% $] varchar (10),
- [Top 100 $] varchar (10),
- [CNH LT] int,
- [On Hand] int,
- [Months OH] decimal,
- [3_Mo Use] int,
- [Avg_Exp_Qty] int,
- [Sfty Stck] decimal,
- [Months Sfty_Stck] decimal,
- [On Order] int,
- [PO Nxt Due] datetime,
- [R_Part_Equiv] varchar (20),
- [R OnHand] int,
- [AvgReqs] decimal,
- [Std] numeric,
- [2019 07] int,
- [2019 08] int,
- [2019 09] int,
- [2019 10] int,
- [rc.2019 07] int,
- [rc.2019 08] int,
- [rc.2019 09] int,
- [rc.2019 10] int,
- [rc.2019 11] int,
- [rc.2019 12] int,
- [rc.2020 01] int,
- [Tot_Reqs] int,
- [Ext Reqs] numeric,
- [% Ext $] varchar(20),
- [pc.2019 07] int,
- [pc.2019 08] int,
- [pc.2019 09] int,
- [pc.2019 10] int,
- [pc.2019 11] int,
- [pc.2019 12] int,
- [pc.2020 01] int,
- [Tot_Open] int,
- [Ext_Open] numeric,
- [PO_LT MIN] int,
- [PO_LT MAX] int,
- [#_PO 12_Mos] int,
- [LTL Cnt] int,
- [Dem_LT MIN] int,
- [Dem_LT MAX] int,
- [Dem_LT AVG] int,
- [3_Mo Job] int,
- [3_Mo Exp] int,
- [Tot_Exp_$$] numeric,
- [pu.Avg_Exp_Qty] numeric,
- [Avg_Exp_$$] numeric,
- )
- Declare @EndDate date = DATEADD(DAY, -1, GETDATE())
- Declare @StartDate date = DATEADD(Month, -3, GETDATE()-1)
- DECLARE @CutOffDate as Date = cast(DATEADD(year, -1, GETDATE()) AS DATE);
- With
- ReqRowsCTE
- AS (
- SELECT wu.[Component_Part]
- , iv.vend_item
- , iv.lead_time
- , iv.vend_num
- , CIM.matl_cost
- , concat(DATEPART(Year,MR.Due_Date), '-', REPLACE(STR(Datepart(month, MR.due_date),2), ' ', '0') ) as Period
- ,Round(sum(wu.Net_qty * MR.rcpt_qty),0) as Qty_Req_int
- FROM rcpts_mst as MR
- JOIN [CNHREMAN_App].[dbo].[CNH_WhereUsed] wu on MR.item = wu.[Finished_Good]
- JOIN [CNHREMAN_App].[dbo].[item_mst] cim on cim.item = wu.component_part
- JOIN [CNHREMAN_App].[dbo].[itemvend_mst] iv on iv.item = cim.item and iv.rank = 1
- WHERE cim.p_m_t_code = 'P' and iv.vend_num in (' 100350',' 100351',' 100356',' 100357')
- GROUP BY Component_Part, iv.vend_item, CIM.matl_cost, cim.description, iv.lead_time, iv.vend_num, DATEPART(Year,MR.Due_Date), DATEPART(Month,MR.Due_Date)
- ),
- ReqColsCTE
- AS (
- Select * From ReqRowsCTE
- Pivot
- (
- Sum(Qty_Req_int)
- For Period in
- (
- [2019-07]
- ,[2019-08]
- ,[2019-09]
- ,[2019-10]
- ,[2019-11]
- ,[2019-12]
- ,[2020-01]
- )
- ) as PivotTable
- --Group by Component_part, Vend_item, lead_time, Vend_num
- ),
- PORowsCTE
- AS (
- SELECT poi.Item
- , Sum(poi.qty_ordered - poi.qty_received) as Open_Qty
- , concat(DATEPART(Year,poi.Due_Date), '-', REPLACE(STR(Datepart(month, poi.due_date),2), ' ', '0') ) as Period_Due
- FROM poitem_mst as POI
- join po_mst as POH
- on poi.po_num = poh.po_num and poh.whse = 'Main'
- and poh.vend_num in (' 100350',' 100351',' 100356',' 100357')
- and poh.po_num not like 'RD%'
- WHERE poi.item like 'N%'
- and poi.qty_ordered > poi.qty_received
- GROUP BY poi.item, DATEPART(Year,poi.Due_Date), DATEPART(Month,poi.Due_Date)
- ),
- POColsCTE
- AS (
- Select * From PORowsCTE
- Pivot
- (
- sum(Open_qty)
- For Period_Due in
- (
- --[2019-04]
- [2019-05]
- ,[2019-06]
- ,[2019-07]
- ,[2019-08]
- ,[2019-09]
- ,[2019-10]
- ,[2019-11]
- ,[2019-12]
- ,[2020-01]
- )
- ) as PivotTable
- --Group by Component_part, Vend_item, lead_time, Vend_num
- ),
- ReceiptSummCTE
- as
- ( SELECT
- POD.po_num AS PO_Num
- , POD.po_line AS PO_Line
- , count(RC.po_num) as Receipts
- , min(RC.rcvd_date) AS [1stRcvdDate]
- , max(RC.rcvd_date) AS [LstRcvdDate]
- , Sum(RC.qty_received) AS [Rcvd.Qty]
- FROM
- poitem_mst POD
- INNER JOIN
- po_mst POM ON POD.site_ref = POM.site_ref AND POD.po_num = POM.po_num and POM.whse = 'Main'
- Inner Join
- vendaddr_mst VN on POM.vend_num = VN.vend_num
- LEFT OUTER JOIN
- po_rcpt_mst RC ON POD.site_ref = RC.site_ref AND POD.po_num = RC.po_num AND POD.po_line = RC.po_line
- WHERE
- POD.item LIKE 'N%' AND (POM.order_date > @CutOffDate) AND (DATEDIFF(day, POM.order_date, POD.due_date) >= 0)
- Group By
- POD.po_num, POD.po_line
- ),
- PODemLTLCTE
- AS (
- SELECT
- POD.item
- ,Count(POD.po_num) as LTLCnt
- ,cast(Sum(POD.qty_ordered) as Int) as LTLTotQty
- ,Cast(Sum(POD.qty_received) as Int) as LTLTotRcvd
- ,Avg(DATEDIFF(day, POM.order_date, RC.[1stRcvdDate])) as LTLDaysAvg
- ,min(DATEDIFF(day, POM.order_date, RC.[1stRcvdDate])) as LTLDaysMin
- ,max(DATEDIFF(day, POM.order_date, RC.[1stRcvdDate])) as LTLDaysMax
- FROM
- poitem_mst as POD
- INNER JOIN
- po_mst POM ON POD.site_ref = POM.site_ref
- AND POD.po_num = POM.po_num
- and pom.vend_num In (' 100350',' 100351',' 100356',' 100357')
- and POM.whse = 'Main'
- LEFT OUTER JOIN
- ReceiptSummCTE RC ON POD.po_num = RC.po_num AND POD.po_line = RC.po_line
- INNER JOIN
- item_mst IM ON POD.site_ref = IM.site_ref AND POD.item = IM.item
- WHERE
- POD.item LIKE 'N%'
- AND POM.order_date > @CutOffDate
- AND DATEDIFF(day, POM.order_date, POD.due_date) >= 0
- and POD.qty_ordered > 0
- and POD.qty_received > 0
- --Check If NOT OT1 and Less than Lead Time
- AND ((DATEDIFF(day, POM.order_date, POD.due_date) > 2)
- and ((DATEDIFF(day, POM.order_date, POD.promise_date) > 2) or POD.promise_date is null)
- and (
- ((POD.Promise_Date IS NOT NULL) and (DATEDIFF(day, POM.order_date, POD.Promise_Date) < IM.lead_time))
- or ((POD.Promise_Date IS NULL) and (DATEDIFF(day, POM.order_date, POD.Due_Date) < IM.lead_time))
- ))
- Group by POD.item
- ),
- POSumCTE
- AS (
- SELECT
- POD.item
- ,Count(POD.po_num) as TotCnt
- ,Cast(Sum(POD.qty_ordered)as Int) as TotQty
- ,Cast(Sum(POD.qty_received) as INT) as TotRcvd
- ,Sum(Cast((POD.qty_ordered - POD.qty_received) as Int)) as TotOnOrder
- ,Avg(DATEDIFF(day, POM.order_date, RC.[1stRcvdDate])) as TotDaysAvg
- ,min(DATEDIFF(day, POM.order_date, RC.[1stRcvdDate])) as TotDaysMin
- ,max(DATEDIFF(day, POM.order_date, RC.[1stRcvdDate])) as TotDaysMax
- FROM
- poitem_mst POD
- INNER JOIN
- po_mst POM ON POD.site_ref = POM.site_ref
- AND POD.po_num = POM.po_num
- and pom.vend_num In (' 100350',' 100351',' 100356',' 100357')
- and POM.whse = 'Main'
- LEFT OUTER JOIN
- ReceiptSummCTE RC ON POD.po_num = RC.po_num AND POD.po_line = RC.po_line
- INNER JOIN
- item_mst IM ON POD.site_ref = IM.site_ref AND POD.item = IM.item
- WHERE
- POD.item LIKE 'N%'
- AND POM.order_date > @CutOffDate
- AND DATEDIFF(day, POM.order_date, POD.due_date) >= 0
- and POD.qty_ordered > 0
- Group by POD.item
- ),
- PONextCTE
- AS (
- SELECT
- POD.item
- ,Count(POD.po_num) as TotCnt
- ,Cast(Min(POD.due_date) as Date) as Next_Due
- ,cast(Min(DATEDIFF(day, POM.order_date, POD.due_date)) as Int) as MIN_PO_LT
- ,cast(Max(DATEDIFF(day, POM.order_date, POD.due_date)) as Int) as MAX_PO_LT
- FROM
- poitem_mst POD
- INNER JOIN
- po_mst POM ON POD.site_ref = POM.site_ref and POM.whse = 'Main'
- AND POD.po_num = POM.po_num
- and pom.vend_num In (' 100350',' 100351',' 100356',' 100357')
- WHERE
- POD.item LIKE 'N%'
- AND POM.order_date > @CutOffDate
- AND DATEDIFF(day, POM.order_date, POD.due_date) >= 0
- and POD.qty_ordered > 0
- --and POD.qty_received = 0
- and POD.qty_received < POD.qty_ordered
- Group by POD.item
- ),
- PartUseCTE
- AS (
- Select
- MT.item
- , Sum(MT.qty * -1) AS [3_Mo_Use]
- , Sum(Case
- When mt.trans_type = 'I'
- Then mt.qty * -1
- Else 0
- End) as [3_Mo_Iss]
- , Sum(Case
- When mt.trans_type = 'G'
- Then mt.qty * -1
- Else 0
- End) as [3_Mo_Exp]
- , Cast(Avg
- (Case
- When mt.trans_type = 'G'
- Then mt.qty * -1
- Else 0
- End)
- as INT) as [Avg_Exp_Qty]
- From matltran_mst MT
- Where
- MT.item like 'N%'
- and mt.trans_date >= @StartDate
- and mt.trans_date <= @EndDate
- and ((mt.trans_type = 'G' and reason_code = 'LST')
- or mt.trans_type = 'I')
- Group By MT.Item
- )
- Insert into CNH_MaterialsRequirementReportTable
- Select
- RC.Component_Part as [CNHR Part]
- ,RC.vend_item as [Vendor Part#]
- , IM.description as Description
- , IM.product_code as PC
- , pcm.description as [PC Description]
- , Coalesce(left(FPT.[Vendor Name], 3), '') as FPT
- , '' as [Top 80% $]
- , '' as [Top 100 $]
- , rc.lead_time as [CNH LT]
- , Cast(IW.qty_on_hand as INT) as [On Hand]
- ,CASE
- When ((ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) / 7) > 0
- Then Round((IW.qty_on_hand /
- (ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) / 7), 1)
- Else 0
- End as [Months OH]
- , Coalesce(Cast(pu.[3_Mo_Use] as INT), 0) as [3_Mo Use]
- , Coalesce(Cast(pu.[Avg_Exp_Qty] as INT), 0) as [Avg_Exp_Qty]
- , Cast(IW.qty_reorder as INT) as [Sfty Stck]
- ,CASE
- When ((ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) / 7) > 0
- Then Round((IW.qty_reorder /
- ((ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) / 7)), 1)
- Else 0
- End as [Months Sfty_Stck]
- ,iw.qty_ordered as [On Order]
- , Coalesce(LEFT(CONVERT(VARCHAR, PN.Next_Due, 120), 10), '') as [PO Nxt Due]
- , Coalesce(concat('R',right(im.item,len(im.item)-1)),'') as [R_Part_Equiv]
- , Coalesce(Cast(riw.qty_on_hand as INT), 0) as [R OnHand]
- , Cast((( + ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) / 7) as decimal(9,2)) as AvgReqs
- --, iw.matl_cost as [Matl Cost]
- , iw.unit_cost as Std
- ,(Cast(IW.qty_on_hand as INT)+ ISNULL(PC.[2019-05],0) + ISNULL(pc.[2019-06],0) + ISNULL(pc.[2019-07],0))
- - (ISNULL(rc.[2019-07],0))
- as [2019 07]
- ,(Cast(IW.qty_on_hand as INT)+ ISNULL(PC.[2019-05],0) + ISNULL(pc.[2019-06],0) + ISNULL(pc.[2019-07],0) + ISNULL(pc.[2019-08],0))
- - (ISNULL(rc.[2019-07],0) + ISNULL(rc.[2019-08],0))
- as [2019 08]
- ,(Cast(IW.qty_on_hand as INT)+ ISNULL(PC.[2019-05],0) + ISNULL(pc.[2019-06],0) + ISNULL(pc.[2019-07],0) + ISNULL(pc.[2019-08],0) + ISNULL(pc.[2019-09],0))
- - (ISNULL(rc.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0))
- as [2019 09]
- ,(Cast(IW.qty_on_hand as INT)+ ISNULL(PC.[2019-05],0) + ISNULL(pc.[2019-06],0) + ISNULL(pc.[2019-07],0) + ISNULL(pc.[2019-08],0) + ISNULL(pc.[2019-09],0) + ISNULL(pc.[2019-10],0))
- - (ISNULL(rc.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0))
- as [2019 10]
- ,Coalesce(RC.[2019-07], '0') as [2019 07]
- ,Coalesce(rc.[2019-08], '0') as [2019 08]
- ,Coalesce(rc.[2019-09], '0') as [2019 09]
- ,Coalesce(rc.[2019-10], '0') as [2019 10]
- ,Coalesce(rc.[2019-11], '0') as [2019 11]
- ,Coalesce(rc.[2019-12], '0') as [2019 12]
- ,Coalesce(RC.[2020-01], '0') as [2020 01]
- ,ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0) as Tot_Reqs
- ,(ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) * iw.matl_cost as [Ext Reqs]
- , '' as [% Ext $]
- ,ISNULL(PC.[2019-07],0) as [2019 07]
- ,ISNULL(PC.[2019-08],0) as [2019 08]
- ,ISNULL(PC.[2019-09],0) as [2019 09]
- ,ISNULL(PC.[2019-10],0) as [2019 10]
- ,ISNULL(PC.[2019-11],0) as [2019 11]
- ,ISNULL(PC.[2019-12],0) as [2019 12]
- ,ISNULL(PC.[2020-01],0) as [2020 01]
- ,ISNULL(PC.[2019-07],0) + ISNULL(PC.[2019-08],0) + ISNULL(PC.[2019-09],0) + ISNULL(PC.[2019-10],0) + ISNULL(PC.[2019-11],0) + ISNULL(PC.[2019-12],0) + ISNULL(PC.[2020-01],0) as Tot_Open
- ,(ISNULL(PC.[2019-07],0) + ISNULL(PC.[2019-08],0) + ISNULL(PC.[2019-09],0) + ISNULL(PC.[2019-10],0) + ISNULL(PC.[2019-11],0) + ISNULL(PC.[2019-12],0) + ISNULL(PC.[2020-01],0)) * IW.matl_cost as Ext_Open
- , Coalesce(PN.MIN_PO_LT,'') as [PO_LT MIN]
- , Coalesce(PN.MAX_PO_LT,'') as [PO_LT MAX]
- --, Coalesce(Cast(POS.TotOnOrder as INT),'0') as OnOrderPO
- , Coalesce(Cast(POS.TotCnt as INT), '0') as [#_PO 12_Mos]
- , Coalesce(pol.LTLCnt, '') as [LTL Cnt]
- , Coalesce(pol.LTLDaysMin, '') as [Dem_LT MIN]
- , Coalesce(pol.LTLDaysMax, '') as [Dem_LT MAX]
- , Coalesce(pol.LTLDaysAvg, '') as [Dem_LT AVG]
- , Coalesce(Cast(pu.[3_Mo_iss] as INT), 0) as [3_Mo Job]
- , Coalesce(Cast(pu.[3_Mo_exp] as INT), 0) as [3_Mo Exp]
- , Coalesce(pu.[3_Mo_exp] * iw.unit_cost,0) as Tot_Exp_$$
- , Coalesce(pu.Avg_Exp_Qty, 0) as [Avg_Exp_Qty]
- , Coalesce(pu.Avg_Exp_Qty * iw.unit_cost, 0) as Avg_Exp_$$
- from ReqColsCTE RC
- Left Join item_mst as IM on RC.Component_Part = IM.item
- Left Join itemwhse_mst as IW on IM.item = IW.item and IW.whse = 'Main'
- Left Join POColsCTE PC on IM.item = PC.item
- Left Join PONextCTE PN on IM.item = PN.item
- Left join PODemLTLCTE as POL on IM.item = POL.item
- Left Join POSumCTE as POS on IM.item = POS.item
- Left Join CNH_FPT_Parts as FPT on IM.Item = FPT.Item
- Left Join PartUseCTE as pu on IM.Item = pu.Item
- Left join item_mst rim on rim.item = concat('R',right(im.item,len(im.item)-1))
- Left join itemwhse_mst riw on riw.item = concat('R',right(im.item,len(im.item)-1)) and riw.whse = 'Main'
- Left join prodcode_mst pcm on im.product_code = pcm.product_code
- Where
- (ISNULL(RC.[2019-07],0) + ISNULL(rc.[2019-08],0) + ISNULL(rc.[2019-09],0) + ISNULL(rc.[2019-10],0) + ISNULL(rc.[2019-11],0) + ISNULL(rc.[2019-12],0) + ISNULL(rc.[2020-01],0)) > 1
- Order by RC.Component_Part
- END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement