Mysql案例声明不起作用?

时间:2018-01-30 07:22:49

标签: mysql pivot

我试图获取特定流程的CycleTime报告。

  

我在这里获得了processOrders的充电时间

select processorder_number,timediff(max(process_end_time), 
min(process_start_time)) as `Charging_time`
from process_tracker where status_id=21 
group by processorder_number;
  

Query的输出就像这样

---------+---------+----------------+
| processorder_number| charging_time| 
+--------------------+--------------+
|   1002135897       | 00:00:16     |
|   1002135897       |02:21:20      |
---------------------+---------------

我在这里获得了processOrders的包装时间

select processorder_number,timediff(max(process_end_time), 
min(process_start_time)) as `Packing_time`
from process_tracker where status_id=14 
group by processorder_number;
  

Query的输出就像这样

---------+---------+----------------+
| processorder_number| Packing_time | 
+--------------------+--------------+
|   1002135897       | 00:11:16     |
|   1002135897       | 01:21:20     |
---------------------+---------------

查询完成时间

select processorder_number,max(process_end_time),min(process_start_time),
timediff(max(process_end_time), min(process_start_time)) as `Complete_time`
from process_tracker where week(process_start_time)=week(curdate())
group by processorder_number
order by processorder_number asc;

我需要这样的输出。

+--------+---------+----------------+----------------+--------------+---------------+
| processorder_number| charging_time| packaging_time | quality_time |Completion_time|
+--------------------+--------------+----------------+--------------+---------------+
|   1002135897       | 00:00:16     |   00:11:16     | 00:21:20     | 00:33:56      |
|   1002135897       | 02:21:20     |   01:21:20     | 00:20:13     | 04:02:53      |
---------------------+--------------+----------------+--------------+---------------+

尝试使用case语句但没有获得输出。只有零作为输出

select processorder_number,
    (CASE WHEN status_id = 21 then timediff(max(process_end_time),min(process_start_time)) else 0 END ) as charging_time,
    (CASE WHEN status_id = 14 then timediff(max(process_end_time),min(process_start_time)) else 0 END ) as Packing_time,
    (CASE WHEN status_id =18 then timediff(max(process_end_time),min(process_start_time)) else 0 END ) as quality_time
    from process_tracker group by processorder_number;
  

以上查询仅生成零作为输出。请帮助我,任何人。   提前谢谢

1 个答案:

答案 0 :(得分:0)

尝试执行以下查询:

select processorder_number,
CASE status_id
        WHEN 21 then timediff(max(process_end_time),min(process_start_time)) 
        ELSE 0 
        END as charging_time,
CASE status_id
        WHEN 14 then timediff(max(process_end_time),min(process_start_time)) 
        ELSE 0 
        END as Packing_time,
CASE status_id
        WHEN 18 then timediff(max(process_end_time),min(process_start_time)) 
        ELSE 0 
        END as quality_time
        from process_tracker group by processorder_number,status_id;