如何为开始日期和结束日期之间的每个工作日创建多行

Posted

技术标签:

【中文标题】如何为开始日期和结束日期之间的每个工作日创建多行【英文标题】:How to create multiple rows for every workingday between an start and end date 【发布时间】:2019-12-17 09:51:01 【问题描述】:

我想在我的开始日期和结束日期之间的每个工作日创建一行。这样生产量就被分配到真正的几周,而不仅仅是开始周。

我现在拥有的:

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

导致:

我想得到下一个结果:

n 行数,其中 N 等于从 start 到 enddate 的工作日数 并且以下列的值分布在这些工作日内

“生产” “需要的金额子部分” “剩余小时数”

添加的列

“运行日期”(工作日日期) “运行年份”(运行日期的年份) “运行周”(运行日期的等周)

我删除了“NoOfWeekDays”列

我已尝试创建 CTE,但无法使其工作。


基于 KumarHarsh 的回答:

我得到以下结果: 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

感谢 KumarHarsch 的最终工作代码。

    with CTE As
(
Select PH.ProdHeaderOrdNr,
      PH.ProdstatusCode,
      PH.partcode,
      PH.description as N'Omschrijving',
      PBOM.subpartcode,
      left(p.partgrpcode,1)as N'artikelgroeptype',
      PBOM.description as N'Draad',
      PBOO.qty,
      PBOO.producedqty,
      PBOO.machcycletime,
      PBOO.MachGrpCode,
      pbom.waste,
      PBOO.qty-PBOO.producedqty as N'ToProduce',

      (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,
      PBOO.startdate as N'Startdate',
      PBOO.enddate as N'Enddate'
From
         ((dbo.T_ProductionHeader  AS PH
         inner join  dbo.T_ProdBillofMat as PBOM on  PH.ProdHeaderDossierCode = PBOM.ProdHeaderDossierCode)
         inner join dbo.T_ProdBillOfOper as PBOO on PH.ProdHeaderDossierCode = PBOO.ProdHeaderDossierCode),
         dbo.T_MachGrp as M,
         dbo.T_part as P  
where

    PBOO.MachGrpCode = M.MachGrpCode AND
    M.DeptCode = '0720' and
    PH.ProdStatusCode in ( 11, 30, 40 ) AND
    PBOM.LineNr = '10' and
    left(PH.partcode,1) in ('P', 'H')and
    PBOO.finishedInd = 0 and
    p.partcode = pbom.subpartcode

    )

Select c.*
,ca.RunningDates,
cf.ndagen,
      datepart(year,ca.RunningDates) as N'Year_Run',
      datepart(isoww,ca.RunningDates) as N'Week_Run',
case when c.producedqty>convert(decimal(6,2),((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2))) * (c.qty/cf.ndagen) then 0
              else (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)) * (c.qty/cf.ndagen))  - (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen)) - (case when c.producedqty>((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen) then (c.producedqty-(((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen))) else 0 end) end
              As ProductionPerWorkDay,
 case when c.producedqty>convert(decimal(6,2),((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2))) * (c.qty/cf.ndagen) then 0
              else (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)) * (c.qty/cf.ndagen))  - (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen)) - (case when c.producedqty>((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen) then (c.producedqty-(((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen))) else 0 end) end *(1+(convert(decimal(4,2),c.waste)/100)) 
              as subpartneededperworkday,
      case when c.machcycletime *((case when c.producedqty>convert(decimal(6,2),((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2))) * (c.qty/cf.ndagen) then 0
              else (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)) * (c.qty/cf.ndagen))  - (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen)) - (case when c.producedqty>((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen) then (c.producedqty-(((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen))) else 0 end) end)/c.qty)/3600<=0 
           then 0 
           else  Round(c.machcycletime *((case when c.producedqty>convert(decimal(6,2),((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2))) * (c.qty/cf.ndagen) then 0
              else (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)) * (c.qty/cf.ndagen))  - (((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen)) - (case when c.producedqty>((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen) then (c.producedqty-(((DATEDIFF(dd, c.startdate, ca.RunningDates) + 1)
              -(DATEDIFF(wk, c.startdate, ca.RunningDates) * 2)-1) * (c.qty/cf.ndagen))) else 0 end) end)/c.qty)/3600,2) 
           end as N'HoursRemaining'           


from
CTE C
cross apply(select cd.capacityavailabledate as RunningDates from dbo.T_machgrpRealCapacity CD where cd.MachGrpCode = c.MachGrpCode and cd.capacityavailabledate>=c.Startdate and cd.capacityavailabledate<=c.enddate and DATENAME(dw, cd.capacityavailabledate) <> 'Sunday' and DATENAME(dw, cd.capacityavailabledate) <> 'Saturday' )ca
cross apply(select count(*) as ndagen from dbo.T_machgrpRealCapacity CD where cd.MachGrpCode = c.MachGrpCode and cd.capacityavailabledate>=c.Startdate and cd.capacityavailabledate<=c.enddate and DATENAME(dw, cd.capacityavailabledate) <> 'Sunday' and DATENAME(dw, cd.capacityavailabledate) <> 'Saturday' )cf

【问题讨论】:

我使用的是 Microsoft SQL Server 2014。(添加到标签中) 【参考方案1】:

首先创建日历表。

以任何你想要的方式填充。

create table CalendarDate(Dates DateTime primary key)
insert into CalendarDate WITH (TABLOCK)  (Dates)
select top (1000000) 
DATEADD(day, ROW_NUMBER()over(order by (select null))-1,'1970-01-01') DT
from sys.objects

假设您的主要查询在 CTE 内(此脚本未经测试)

With CTE As
(
 Select ,
DATEDIFF(dd, PBOO.startdate, PBOO.enddate) + 1 as Diff
From 
)

Select c.*
,ca.RunningDates 
'to produce'/Datediff
'amount subpart needed'/Datediff
from
CTE C
cross apply(select Dates as RunningDates from CalendarDate CD where cd.dates>=c.Startdate and cd.dates<=c.enddate)ca

这回答了一个重要部分。如果这是正确的,那么您可以继续进行。

我希望我的改变很清楚。

像“toproduce”这样的别名是错误的,它应该是一个词 ToProduce。

编辑 1,

create table #temp (startdt datetime,enddate datetime)
insert into #temp values('2010-01-01','2010-01-05')
,('2011-02-01','2011-02-06')

select t.*,ca.RunningDate from #temp t
cross apply(select cd.Dates as RunningDate 
from CalendarDate cd 
where cd.Dates>=t.startdt and cd.Dates<=t.enddate)ca

select t.*,cd.Dates as RunningDate from #temp t
inner join  CalendarDate cd 
on cd.Dates>=t.startdt and cd.Dates<=t.enddate

drop table #temp

检查你自己,它按预期工作正常。

交叉应用但结果只显示第一行 无休止地使用相同的运行日期值

这不是因为CROSS APPLY。原因一定是别的东西。调试并找到它。 you.Once 评论 CROSS APPLY 部分并检查结果或验证值 Calendar Table。

dbo.T_ProductionHeader 作为 PH,dbo.T_ProdBillofMat 作为 PBOM,

不好的做法。明确提及 JOIN 语法。减少意外笛卡尔积连接的风险。逗号连接在上个世纪是如此流行。它也让其他人更容易理解。

dbo.T_ProductionHeader  AS PH inner join
 dbo.T_ProdBillofMat as PBOM on PH.ProdHeaderDossierCode = PBOM.ProdHeaderDossierCode    AND
    PH.ProdHeaderDossierCode = PBOO.ProdHeaderDossierCode AND

将额外的过滤器放在 where 子句上,例如PH.ProdStatusCode BETWEEN 30 and 40

【讨论】:

您好 KumarHarsch,感谢您的回复。我已经有一个日历表,所有日期值都是 datetime2。我将别名更改为一个单词。计算 46.216231.08 / 5 --> 可能超过 24 小时,它只是意味着更多的机器。我得到了计算工作。然而,交叉应用导致只有第一行无限显示,runningdate 的值与 startdate 相同。我将显示我当前的代码并导致上面的问题

以上是关于如何为开始日期和结束日期之间的每个工作日创建多行的主要内容,如果未能解决你的问题,请参考以下文章

计算员工在开始和结束日期之间应报告工作的天数

如何为用户返回每月最大日期

MS Access:按开始日期和结束日期之间每个月的月份分组

Postgresql 查询以展开每个客户的开始日期和结束日期之间的所有日期

sql server 中两个日期之间的年份以及每个日期在 sql server 中的开始和结束日期

两个字段中两个日期之间的日期