SET @INDEX1=0;
SET @INDEX2=0;
SELECT d1.UserCD,d1.OperationDT,d1.CustomerCD,d1.OperationCD,d1.ConstructionNO,
d1.OperationDT,d2.OperationDT,
#TIMEDIFF(d2.OperationDT,IFNULL(d1.OperationDT,d2.OperationDT)) As UseTime
TIMEDIFF(d2.OperationDT,d1.OperationDT) As UseTime
FROM
(
SELECT UserCD,OperationDT,CustomerCD,OperationCD,ConstructionNO,
(@INDEX1 := @INDEX1 + 1) AS IdxNum
FROM daily_reports
WHERE DeleteFLG = 0
Order by UserCD,OperationDT
) AS d1
INNER JOIN
(
SELECT UserCD,OperationDT,CustomerCD,OperationCD,ConstructionNO,
(@INDEX2 := @INDEX2 + 1)-1 AS IdxNum
FROM daily_reports
WHERE DeleteFLG = 0
Order by UserCD,OperationDT
) AS d2
ON d1.ConstructionNO = 10
AND d1.IdxNum = d2.IdxNum
AND d1.UserCD = d2.UserCD以上是我的Sql代码,具体想实现举个例子说下吧
时间 作业 干活种类
OperationDT OperationNM ConstructionNO
12:00 移动 10
13:00 工事 10
14:00 移动 NULL
15:00 回社 NULL
16:00 移动 10
17:00 工事 10
18:00 回家 NULL现在假如就这一个人行程,时间(开始的时间)和作业就不说了,后面的干活的种类那个10别管他是什么就是去干那种活了,NULL表示没有活了,比如14点的移动就是要回公司了没有活了。我以上的sql已经可以做到计算它们之间所需要时间了,我是这么做的,在select表后又增加一列IdxNum,我写了2个select,是为了比较他们的值用的d1
OperationDT OperationNM ConstructionNO IdxNum
12:00 移动 10 1
13:00 工事 10 2
14:00 移动 NULL 3
15:00 回社 NULL 4
16:00 移动 10 5
17:00 工事 10 6
18:00 回家 NULL 7d2
OperationDT OperationNM ConstructionNO IdxNum
12:00 移动 10 0
13:00 工事 10 1
14:00 移动 NULL 2
15:00 回社 NULL 3
16:00 移动 10 4
17:00 工事 10 5
18:00 回家 NULL 6然后d1.IdxNum = d2.IdxNum
这样我可以经过错位计算移动用了多少时间,工事用了多少时间。
现在看数据这么计算是没有问题,但是假如把18:00回家数据删除后,
由于d1.IdxNum = d2.IdxNum,d1表中17:00工事的IdxNum是6
而d2表中的IdxNum最高也就5,匹配不上,也就是说17:00工事不能被显示(查不出来),我想有没有办法,把17:00工事也查出来,只是计算时间这个部分这个时间只是个开始时间而已,结束时间还不知道,可以设置成0。
SET @INDEX2=0;
SELECT d1.UserCD,d1.OperationDT,d1.CustomerCD,d1.OperationCD,d1.ConstructionNO,
d1.OperationDT,d2.OperationDT,
#TIMEDIFF(d2.OperationDT,IFNULL(d1.OperationDT,d2.OperationDT)) As UseTime
TIMEDIFF(d2.OperationDT,d1.OperationDT) As UseTime
FROM
(
SELECT UserCD,OperationDT,CustomerCD,OperationCD,ConstructionNO,
(@INDEX1 := @INDEX1 + 1) AS IdxNum
FROM daily_reports
WHERE DeleteFLG = 0
Order by UserCD,OperationDT
) AS d1
INNER JOIN
(
SELECT UserCD,OperationDT,CustomerCD,OperationCD,ConstructionNO,
(@INDEX2 := @INDEX2 + 1)-1 AS IdxNum
FROM daily_reports
WHERE DeleteFLG = 0
Order by UserCD,OperationDT
) AS d2
ON d1.ConstructionNO = 10
AND d1.IdxNum = d2.IdxNum
AND d1.UserCD = d2.UserCD以上是我的Sql代码,具体想实现举个例子说下吧
时间 作业 干活种类
OperationDT OperationNM ConstructionNO
12:00 移动 10
13:00 工事 10
14:00 移动 NULL
15:00 回社 NULL
16:00 移动 10
17:00 工事 10
18:00 回家 NULL现在假如就这一个人行程,时间(开始的时间)和作业就不说了,后面的干活的种类那个10别管他是什么就是去干那种活了,NULL表示没有活了,比如14点的移动就是要回公司了没有活了。我以上的sql已经可以做到计算它们之间所需要时间了,我是这么做的,在select表后又增加一列IdxNum,我写了2个select,是为了比较他们的值用的d1
OperationDT OperationNM ConstructionNO IdxNum
12:00 移动 10 1
13:00 工事 10 2
14:00 移动 NULL 3
15:00 回社 NULL 4
16:00 移动 10 5
17:00 工事 10 6
18:00 回家 NULL 7d2
OperationDT OperationNM ConstructionNO IdxNum
12:00 移动 10 0
13:00 工事 10 1
14:00 移动 NULL 2
15:00 回社 NULL 3
16:00 移动 10 4
17:00 工事 10 5
18:00 回家 NULL 6然后d1.IdxNum = d2.IdxNum
这样我可以经过错位计算移动用了多少时间,工事用了多少时间。
现在看数据这么计算是没有问题,但是假如把18:00回家数据删除后,
由于d1.IdxNum = d2.IdxNum,d1表中17:00工事的IdxNum是6
而d2表中的IdxNum最高也就5,匹配不上,也就是说17:00工事不能被显示(查不出来),我想有没有办法,把17:00工事也查出来,只是计算时间这个部分这个时间只是个开始时间而已,结束时间还不知道,可以设置成0。
那不就乱了,大哥先仔细看下我的SQL语句吧
我那个SQL语句(18点那条不删除的情况下)其实查出来的只有4条
12:00 移动 10
13:00 工事 10
16:00 移动 10
17:00 工事 10
如果用外连接那就全查出来了,我要的是去干活的纪录,并且能计算它们之间的时间
12:00:00 移动 10 1 13:00:00 工事 10 1
13:00:00 工事 10 2 14:00:00 移动 NULL 2
14:00:00 移动 NULL 3 15:00:00 回社 NULL 3
15:00:00 回社 NULL 4 16:00:00 移动 10 4
16:00:00 移动 10 5 17:00:00 工事 10 5
17:00:00 工事 10 6 18:00:00 回家 NULL 6
18:00:00 回家 NULL 7 这种结果?
12:00:00 移动 10 1 13:00:00 工事 10 1
13:00:00 工事 10 2 14:00:00 移动 NULL 2
16:00:00 移动 10 5 17:00:00 工事 10 5
17:00:00 工事 10 6 修改成LEFT JOIN后的结果
ON d1.ConstructionNO = 10
AND d1.IdxNum = d2.IdxNum
AND d1.UserCD = d2.UserCD应该写成
ON d1.IdxNum = d2.IdxNum
AND d1.UserCD = d2.UserCD
Where d1.ConstructionNO = 10最后没有筛选~
谢谢~~~
这个怎么办。。 晕倒