SET @INDEX1 = 0;
SET @INDEX2 = 0;
SET @PRVE1 = 0;
SET @PRVE2 = 0;
SELECT
d1.OperationDT As StartDate,d1.UserNM,d1.CustomerNM,d1.DetailCD,d1.OperationNM,d1.Memo,
d2.OperationDT As EndDate, TIMEDIFF(d2.OperationDT,d1.OperationDT) As UseTime
FROM
(
SELECT d.UserCD,d.OperationDT,d.ConstructionNO,
       u.UserNM,c.CustomerNM,o.DetailCD,o.OperationNM,d.Memo,
       @INDEX1 := if(@PRVE1=d.UserCD,@INDEX1+1,1) AS IdxNum,@PRVE1:=d.UserCD
FROM daily_reports d
LEFT OUTER JOIN users AS u ON d.UserCD = u.UserCD
LEFT OUTER JOIN customers AS c ON d.CustomerCD = c.CustomerCD
LEFT OUTER JOIN operations AS o ON d.OperationCD = o.OperationCD
ORDER BY d.UserCD,d.OperationDT  ) AS d1
LEFT OUTER JOIN
(
SELECT UserCD,OperationDT,ConstructionNO,
       (@INDEX2 := if(@PRVE2=UserCD,@INDEX2+1,1))-1 AS IdxNum,@PRVE2:=UserCD
FROM daily_reports
ORDER BY UserCD,OperationDT  ) AS d2
ON d1.IdxNum = d2.IdxNum
AND d1.UserCD = d2.UserCD
WHERE d1.ConstructionNO = 9531我写的SQL和我要的结果不一致表
人     时间    作业  工作号   
A      12:00   移动  9531     
A      13:00   工事  9531     
A      14:00   回家  NULL     
B      10:30   移动  9531     得出想要得结果是
A  移动  1:00
A  工事  1:30
B  移动  NULL就是A移动了1小时 工事干了1小时半  B移动只有开始时间  没有结束时间  所以为NULL
假如表是这样的
人     时间    作业  工作号   
A      12:00   移动  9531     
A      13:00   工事  9531     
A      14:00   移动  9532    
B      10:30   移动  9531     因为工作号是9532所以  上面一行的工事业就没有了结束时间  所以工事的所用时间为NULL
但是现在把9532当成A      13:00   工事  9531  的结束时间了...
应该是
A  移动  1:00
A  工事  NULL
B  移动  NULL怎么分开来算?

解决方案 »

  1.   

    mysql> select * from t_chopper7278;
    +------+---------------------+------+-------+
    | pid  | t1                  | job  | jobid |
    +------+---------------------+------+-------+
    | A    | 2009-06-08 12:00:00 | 移动 |  9531 |
    | A    | 2009-06-08 13:00:00 | 工事 |  9531 |
    | A    | 2009-06-08 14:00:00 | 回家 |  NULL |
    | B    | 2009-06-08 10:30:00 | 移动 |  9531 |
    +------+---------------------+------+-------+
    4 rows in set (0.00 sec)
    mysql> select pid,job,t1,
        ->  TIMEDIFF((select min(t1) from t_chopper7278 where pid=a.pid and t1>a.t1),t1) as duration
        -> from t_chopper7278 a
        -> order by pid,t1;
    +------+------+---------------------+----------+
    | pid  | job  | t1                  | duration |
    +------+------+---------------------+----------+
    | A    | 移动 | 2009-06-08 12:00:00 | 01:00:00 |
    | A    | 工事 | 2009-06-08 13:00:00 | 01:00:00 |
    | A    | 回家 | 2009-06-08 14:00:00 | NULL     |
    | B    | 移动 | 2009-06-08 10:30:00 | NULL     |
    +------+------+---------------------+----------+
    4 rows in set (0.00 sec)mysql>
      

  2.   

    没注意到你的任务号也要一样。改成下面的试一下。mysql> select pid,job,t1,
        ->  TIMEDIFF((select min(t1) from t_chopper7278 where pid=a.pid and jobid=a.jobid and t1>a.t1),t1) as duration
        -> from t_chopper7278 a
        -> order by pid,t1;
    +------+------+---------------------+----------+
    | pid  | job  | t1                  | duration |
    +------+------+---------------------+----------+
    | A    | 移动 | 2009-06-08 12:00:00 | 01:00:00 |
    | A    | 工事 | 2009-06-08 13:00:00 | NULL     |
    | A    | 回家 | 2009-06-08 14:00:00 | NULL     |
    | B    | 移动 | 2009-06-08 10:30:00 | NULL     |
    +------+------+---------------------+----------+
    4 rows in set (0.00 sec)mysql>
      

  3.   

    或者改你的代码为SET @INDEX2 = 0;
    SET @PRVE2 = 0;
    SET @CNO2 = 0;SELECT UserCD,OperationDT,ConstructionNO,
           (@INDEX2 := if(@PRVE2=UserCD and  @CNO2=ConstructionNO,@INDEX2+1,1))-1 AS IdxNum,@PRVE2:=UserCD, @CNO2:=ConstructionNO
    FROM daily_reports
    ORDER BY UserCD,OperationDT 然后两个了查询联接的时候再加上ON d1.IdxNum = d2.IdxNum
    AND d1.UserCD = d2.UserCD
    and d1.ConstructionNO = d2.ConstructionNO
    个人建议,这种功能应该由程序来直接实现,或在你的宿主程序中编程实现,或者在数据库的存储过程中实现。推荐业务层的东西在程序中,而不是数据库中实现。
      

  4.   

    就是A移动了1小时 工事干了1小时半  B移动只有开始时间  没有结束时间  所以为NULL 
    假如表是这样的 
    人    时间    作业  工作号  
    A      12:00  移动  9531    
    A      13:00  工事  9531    
    A      14:00  移动  9532    
    B      10:30  移动  9531    因为工作号是9532所以  上面一行的工事业就没有了结束时间  所以工事的所用时间为NULL 
    但是现在把9532当成A      13:00  工事  9531  的结束时间了... 
    应该是 
    A  移动  1:00 
    A  工事  NULL 
    B  移动  NULLSELECT *,timediff(b.时间,a.时间) FROM TTA A left JOIN TTA B
    ON A.工作号=B.工作号 AND B.作业='工事' AND A.作业='移动' AND A.人=B.人 
    where a.工作号=9531
      

  5.   

    or
    SELECT *,timediff(b.时间,a.时间) FROM TTA A left JOIN TTA B
    ON A.工作号=B.工作号 AND B.作业='工事' AND A.作业='移动' AND A.人=B.人