我希望在开始日期和结束日期之间的每个可行的工作日创建一行。这样一来,生产量就可以分配到实际的周中,而不仅仅是起始周。
我现在拥有的是:
SELECT DISTINCT
PH.ProdHeaderOrdNr,
PH.ProdstatusCode,
PH.partcode,
PH.description,
PBOM.subpartcode,
PBOM.description as N'Description subpart',
PBOO.qty,
PBOO.producedqty,
PBOO.machcycletime,
PBOO.qty-PBOO.producedqty as N'to produce',
case when(PBOO.qty-PBOO.producedqty)*(1+(convert(decimal(4,2),PBOM.waste)/100))<=0
then 0
else (PBOO.qty-PBOO.producedqty)*(1+(convert(decimal(4,2),PBOM.waste)/100))
end as N'amount subpart needed',
(DATEDIFF(dd, PBOO.startdate, PBOO.enddate) + 1)
-(DATEDIFF(wk, PBOO.startdate, PBOO.enddate) * 2)
-(CASE WHEN DATENAME(dw, PBOO.startdate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, PBOO.enddate) = 'Saturday' THEN 1 ELSE 0 END)
As NoOfWeekDays,
case when PBOO.machcycletime *((PBOO.qty-PBOO.producedqty)/PBOO.qty)/3600<=0
then 0
else Round(PBOO.machcycletime *((PBOO.qty-PBOO.producedqty)/PBOO.qty)/3600,2)
end as N'hours remaining',
PBOO.startdate,
PBOO.enddate,
datepart(year,PBOO.startdate) as N'Year',
datepart(isoww,PBOO.startdate) as N'Week'
FROM
dbo.T_ProductionHeader AS PH,
dbo.T_ProdBillofMat as PBOM,
dbo.T_Part as P,
dbo.T_ProdBillOfOper as PBOO,
dbo.T_MachGrp as M
Where
PH.ProdHeaderDossierCode = PBOM.ProdHeaderDossierCode AND
PH.ProdHeaderDossierCode = PBOO.ProdHeaderDossierCode AND
PBOO.MachGrpCode = M.MachGrpCode AND
M.DeptCode = '0720' and
PH.ProdStatusCode BETWEEN 30 and 40 AND
PBOM.LineNr = '10' and
left(PH.partcode,1) in ('P', 'H')and
PBOO.finishedInd = 0
结果:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS96aGJaRS5wbmcifQ==” alt =“每个生产订单有1行的结果表”>
我想得到下一个结果:
n行数,其中N等于从开始到结束日期的工作日数并且以下各列的值分布在那些工作日内
添加的列
我删除了“ NoOfWeekDays”列
我曾尝试创建CTE,但无法正常工作。我得到以下结果:result based on KumarHarsh
这是我当前的sql代码:
With CTE As
(
Select PH.ProdHeaderOrdNr,
PH.ProdstatusCode,
PH.partcode,
PH.description,
PBOM.subpartcode,
PBOM.description as N'DescriptionSubpart',
PBOO.qty,
PBOO.producedqty,
PBOO.machcycletime,
PBOO.qty-PBOO.producedqty as N'ToProduce',
case when(PBOO.qty-PBOO.producedqty)*(1+(convert(decimal(4,2),PBOM.waste)/100))<=0
then 0
else (PBOO.qty-PBOO.producedqty)*(1+(convert(decimal(4,2),PBOM.waste)/100))
end as N'AmountSubpartNeeded',
(DATEDIFF(dd, PBOO.startdate, PBOO.enddate) + 1)
-(DATEDIFF(wk, PBOO.startdate, PBOO.enddate) * 2)
-(CASE WHEN DATENAME(dw, PBOO.startdate) = 'Sunday' THEN 1 ELSE 0 END)
-(CASE WHEN DATENAME(dw, PBOO.enddate) = 'Saturday' THEN 1 ELSE 0 END)
As NoOfWeekDays,
case when PBOO.machcycletime *((PBOO.qty-PBOO.producedqty)/PBOO.qty)/3600<=0
then 0
else Round(PBOO.machcycletime *((PBOO.qty-PBOO.producedqty)/PBOO.qty)/3600,2)
end as N'HoursRemaining',
PBOO.startdate as N'Startdate',
PBOO.enddate as N'Enddate',
datepart(year,PBOO.startdate) as N'Year',
datepart(isoww,PBOO.startdate) as N'Week'
From
dbo.T_ProductionHeader AS PH,
dbo.T_ProdBillofMat as PBOM,
dbo.T_Part as P,
dbo.T_ProdBillOfOper as PBOO,
dbo.T_MachGrp as M
where
PH.ProdHeaderDossierCode = PBOM.ProdHeaderDossierCode AND
PH.ProdHeaderDossierCode = PBOO.ProdHeaderDossierCode AND
PBOO.MachGrpCode = M.MachGrpCode AND
M.DeptCode = '0720' and
PH.ProdStatusCode BETWEEN 30 and 40 AND
PBOM.LineNr = '10' and
left(PH.partcode,1) in ('P', 'H')and
PBOO.finishedInd = 0
)
Select c.*
,ca.RunningDates,
c.toproduce/c.NoOfWeekDays as N'ProductionPerWorkDay',
c.amountsubpartneeded/c.NoOfWeekDays as N'AmountSubPartNeededPerWorkDay',
c.HoursRemaining/c.NoOfWeekDays as N'HoursRemainingPerWorkDay'
from
CTE C
cross apply(select cd.yeardate as RunningDates from dbo.T_DayOfYear CD where cd.yeardate>=c.Startdate and cd.yeardate<=c.enddate)ca
我希望在开始日期和结束日期之间的每个可行的工作日创建一行。这样一来,生产量就可以分配给实际的几周,而不仅仅是起始周。我现在拥有的是:...