跨不同班次创建每小时计数
Creating Hourly Counts Across Different Shifts
我正在尝试创建一个查询,将班次生产划分为每小时计数,可以 运行 通过多个班次,但每个班次的前几个小时都属于相同的计数。
Sum(CASE WHEN DATEPART(HOUR,[creation_time]) = '1' THEN quantity_increment_D ELSE 0 END) C1,
Sum(CASE WHEN DATEPART(HOUR,[creation_time]) = '2' THEN quantity_increment_D ELSE 0 END) C2,
Sum(CASE WHEN DATEPART(HOUR,[creation_time]) = '3' THEN quantity_increment_D ELSE 0 END) C3,
在我的报告中,此查询适用于当天的第一班,但第二班和第三班不会在 C1 上重新开始。我基本上需要问一些问题:
If(shift_id_S)='1' then sum(quantity_increment_D) where (creation_time) between '00:00:00' and'07:59:59' as C1
If(shift_id_S)='2' then sum(quantity_increment_D) where (creation_time) between '08:00:00' and'15:59:59' as C1
If(shift_id_S)='3' then sum(quantity_increment_D) where (creation_time) between '16:00:00' and'23:59:59' as C1
If(shift_id_S)='4' then sum(quantity_increment_D) where (creation_time) between '00:00:00' and'11:59:59' as C1
If(shift_id_S)='5' then sum(quantity_increment_D) where (creation_time) between '12:00:00' and'23:59:59' as C1
如果我能得到这部分查询的帮助,我就能处理其余的计数。
我不确定你问我们什么问题。你只需要弄清楚逻辑吗?你的第二个查询似乎与你对第一个查询所做的不匹配。
您可以将您的其他条件添加到您的 CASE 语句中,并且有时可以使用另一个 CASE,这样您就不需要单独的条件了。我用得不多 MySQL 但这应该有用:
Sum(CASE WHEN DATEPART(HH, [creation_time]) BETWEEN
CASE WHEN shift_id_S ='1' OR shift_id_S ='4' THEN '00'
WHEN shift_id_S ='2' THEN '08'
WHEN shift_id_S ='3' THEN '16'
WHEN shift_id_S ='5' THEN '12' END
AND
CASE WHEN shift_id_S ='3' OR shift_id_S ='5' THEN '23'
WHEN shift_id_S ='1' THEN '07'
WHEN shift_id_S ='2' THEN '15'
WHEN shift_id_S ='4' THEN '11' END
THEN quantity_increment_D ELSE 0 END) AS C1
然后将此复制到其他班次(C2、C3...)。
我正在尝试创建一个查询,将班次生产划分为每小时计数,可以 运行 通过多个班次,但每个班次的前几个小时都属于相同的计数。
Sum(CASE WHEN DATEPART(HOUR,[creation_time]) = '1' THEN quantity_increment_D ELSE 0 END) C1,
Sum(CASE WHEN DATEPART(HOUR,[creation_time]) = '2' THEN quantity_increment_D ELSE 0 END) C2,
Sum(CASE WHEN DATEPART(HOUR,[creation_time]) = '3' THEN quantity_increment_D ELSE 0 END) C3,
在我的报告中,此查询适用于当天的第一班,但第二班和第三班不会在 C1 上重新开始。我基本上需要问一些问题:
If(shift_id_S)='1' then sum(quantity_increment_D) where (creation_time) between '00:00:00' and'07:59:59' as C1
If(shift_id_S)='2' then sum(quantity_increment_D) where (creation_time) between '08:00:00' and'15:59:59' as C1
If(shift_id_S)='3' then sum(quantity_increment_D) where (creation_time) between '16:00:00' and'23:59:59' as C1
If(shift_id_S)='4' then sum(quantity_increment_D) where (creation_time) between '00:00:00' and'11:59:59' as C1
If(shift_id_S)='5' then sum(quantity_increment_D) where (creation_time) between '12:00:00' and'23:59:59' as C1
如果我能得到这部分查询的帮助,我就能处理其余的计数。
我不确定你问我们什么问题。你只需要弄清楚逻辑吗?你的第二个查询似乎与你对第一个查询所做的不匹配。
您可以将您的其他条件添加到您的 CASE 语句中,并且有时可以使用另一个 CASE,这样您就不需要单独的条件了。我用得不多 MySQL 但这应该有用:
Sum(CASE WHEN DATEPART(HH, [creation_time]) BETWEEN
CASE WHEN shift_id_S ='1' OR shift_id_S ='4' THEN '00'
WHEN shift_id_S ='2' THEN '08'
WHEN shift_id_S ='3' THEN '16'
WHEN shift_id_S ='5' THEN '12' END
AND
CASE WHEN shift_id_S ='3' OR shift_id_S ='5' THEN '23'
WHEN shift_id_S ='1' THEN '07'
WHEN shift_id_S ='2' THEN '15'
WHEN shift_id_S ='4' THEN '11' END
THEN quantity_increment_D ELSE 0 END) AS C1
然后将此复制到其他班次(C2、C3...)。