我有这个代码
select no1, visit, DATE1, TIME1, STATUS ,hour(diff) * 60 + minute(diff) as diff from(
select no1, visit, DATE1,
if(
@active or STATUS in ('Pending Active') and STATUS not in ('Stop Clock') ,
timediff(addtime(DATE1,TIME1),@prevTime),0) as diff,
time(@prevTime := addtime(DATE1,TIME1)) as time1,
@active := STATUS in ('Pending Active') and STATUS not in ('Stop Clock'),STATUS from detail,
(select @prevTime :=0,@active :=false) as init where NO_LOG = '03/12/008' and visit = 'On Site Visit' order by no1) as final
,结果如下:
+------+--------------+------------+---------+-------------------+-----+
| no1 | visit | DATE1 | TIME1 | STATUS | diff|
+------+--------------+------------+---------+-------------------+-----+
|74030 |On Site Visit | 2012-03-12 |19:23:00 | Pending | 0 |
|74031 |On Site Visit | 2012-03-12 |19:44:00 | Pending | 0 |
|74032 |On Site Visit | 2012-03-12 |20:40:00 | Pending Active | 56 |
|74033 |On Site Visit | 2012-03-12 |20:45:00 | Pending Active | 5 |
|74034 |On Site Visit | 2012-03-12 |20:50:00 | Pending Active | 5 |
|74035 |On Site Visit | 2012-03-12 |20:54:00 | Active | 4 |
|74036 |On Site Visit | 2012-03-12 |21:30:00 | Close | 36 |
但是我想要的是,如果访问=现场访问和STATUS =待定活动(第一个待定活动),则第一个待定活动以下的所有deff将为'0'(零)...如下例所示
+------+--------------+------------+---------+-------------------+-----+
| no1 | visit | DATE1 | TIME1 | STATUS | diff|
+------+--------------+------------+---------+-------------------+-----+
|74030 |On Site Visit | 2012-03-12 |19:23:00 | Pending | 0 |
|74031 |On Site Visit | 2012-03-12 |19:44:00 | Pending | 0 |
|74032 |On Site Visit | 2012-03-12 |20:40:00 | Pending Active | 56 |
|74033 |On Site Visit | 2012-03-12 |20:45:00 | Pending Active | 0 |
|74034 |On Site Visit | 2012-03-12 |20:50:00 | Pending Active | 0 |
|74035 |On Site Visit | 2012-03-12 |20:54:00 | Active | 0 |
|74036 |On Site Visit | 2012-03-12 |21:30:00 | Close | 0 |
也许有人可以帮助我..谢谢
答案 0 :(得分:0)
我目前无法提出查询,但我的解决方案是使用UNION ALL
并使用WHERE Status =
过滤并让第二(或第三)UNION
组拥有diff
上的值为0。
<强>代码:强>
SELECT *
FROM ( SELECT no1, visit, DATE1, TIME1, STATUS, 0 AS diff
FROM detail
WHERE status IN ('Pending') AND NO_LOG = '03/12/008' AND visit = 'On Site Visit'
UNION ALL
SELECT no1, visit, DATE1, TIME1, STATUS, # do the diff calculation here
FROM detail
WHERE status IN ('Pending Active') AND NO_LOG = '03/12/008' AND visit = 'On Site Visit'
LIMIT 1
UNION ALL
SELECT no1, visit, DATE1, TIME1, STATUS, 0
FROM detail
WHERE status IN ('Pending Active', 'Active', 'Close') AND NO_LOG = '03/12/008' AND visit = 'On Site Visit'
LIMIT 1, 9999) AS h
ORDER BY no1 ASC