在不同的班次中创造每小时的计数

时间:2015-05-05 18:42:19

标签: sql reporting-services mysqli sum ssms

我正在尝试创建一个查询,将轮班生产分为可以通过多班次运行的每小时计数,但每个班次的前几个小时都属于相同的数量。

    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

如果我可以获得这部分查询的帮助,我可以处理其余的计数。

1 个答案:

答案 0 :(得分:0)

我不确定你的问题是什么。你只需要找出逻辑吗?您的第二个查询似乎与您对第一个查询的查询不匹配。

您可以将其他条件添加到CASE语句中,也可以使用其他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 ......)。