如何将它们分组在两个不同的组中,仅使用单列

时间:2013-08-16 01:40:03

标签: sql-server sql-server-2008 tsql sybase powerbuilder

如何将它们分组在两个不同的组中,只使用单列

示例数据:

date_entry  time_start  time_finished idle_code    qty_good
8/8/2013    13:00       13:30            6            10     
8/8/2013    13:30       15:20            0            20
8/8/2013    15:20       15:30            6            5 
8/8/2013    15:30       16:25            0            10
8/8/2013    16:25       16:40            7            0
8/8/2013    16:40       17:25            0            40
8/8/2013    17:25       17:40            3            10
8/8/2013    17:40       24:00            1
8/8/2013    24:00       00:00            1
8/8/2013    00:00       00:30            1

结果

                             idle_code   total mins.
        Idle time   #1  410:00  mins
        Idle time   #2  0:00    mins
        Idle time   #3  15:00   mins
        Idle time   #4  0:00    mins
        Idle time   #5  0:00    mins
        Idle time   #7  15:00   mins
        Idle time      #0,6     250:00  mins

(A) TOTAL IDLE TIME         440:00mins(idle time #0,6 do not include)
(B) TOTAL OPERATION TIME        250:00mins(idle time #0,6 only)

如何获得idle_code 1的总和或者如何对它们进行分组以得到它的总和?

示例sql

SELECT 
     dbo.t_monitoring_pi_oper_entry.wo_number,   
     dbo.t_monitoring_pi_oper_entry.qty_rejected,   
     dbo.t_monitoring_pi_oper_entry.operator,   
     dbo.t_monitoring_pi_oper_entry.reject_code,   
     dbo.t_monitoring_pi_oper_entry.qty_good,   
     dbo.t_monitoring_pi_oper_entry.idle_code,   
     dbo.t_monitoring_pi_oper_entry.time_finished,   
     dbo.t_monitoring_pi_oper_entry.time_start,   
     dbo.t_monitoring_pi_oper_entry.date_entry  

FROM dbo.t_monitoring_pi_oper_entry   

3 个答案:

答案 0 :(得分:0)

     SELECT SUM(dbo.t_monitoring_pi_oper_entry.time_finished,1,0)  
     FROM dbo.t_monitoring_pi_oper_entry 
     GROUP BY idle_code 

答案 1 :(得分:0)

你可以尝试

CASE WHEN idle_code IN(0,6)那么'0,6'ELSE CAST(idle_code AS varchar)END

SELECT '#' +  CASE WHEN idle_code IN (0,6) THEN '0,6' ELSE CAST(idle_code AS varchar) END  AS  idle_code,
    SUM( DATEDIFF(ss, time_start, time_finished) )
FROM dbo.t_monitoring_pi_oper_entry
GROUP BY '#' +  CASE WHEN idle_code IN (0,6) THEN '0,6' ELSE CAST(idle_code AS varchar) END

答案 2 :(得分:0)

我不确定我是否理解完整的要求,但你绝对可以尝试这样的事情。它与Dante Ditan发布的内容类似,但计算空闲时间除外。

SELECT idle_code, SUM(DATEDIFF(minute, time_start, time_finished))
FROM dbo.t_monitoring_pi_oper_entry  
GROUP BY idle_code

您可以通过添加

来扩展它
WHERE idle_code in (0,6)