【问题标题】:Creating Hourly Counts Across Different Shifts跨不同班次创建每小时计数
【发布时间】:2020-05-30 01:26:09
【问题描述】:

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

    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

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

【问题讨论】:

    标签: sql reporting-services sum ssms


    【解决方案1】:

    我不确定您的问题是什么。你只需要弄清楚逻辑吗?不过,您的第二个查询似乎与您对第一个查询所做的不匹配。

    您可以将您的其他条件添加到您的 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...)。

    【讨论】:

    • 感谢您的帮助。是的,我正在写两个不同的查询。我写了第一个以确保我正在提取写入数据,第二个我无法弄清楚要编写的正确语法。再次感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-30
    • 1970-01-01
    • 2020-06-12
    • 2022-01-25
    • 1970-01-01
    • 2023-04-02
    • 1970-01-01
    相关资源
    最近更新 更多