表位:
+----+-----------+-----------+---------------------+
| Id | Old Value | New Value | Date |
+----+-----------+-----------+---------------------+
| 1 | Home | Work | 2016-11-01 09:00:00 |
| 2 | Work | Home | 2016-11-01 09:25:00 |
| 3 | Home | Work | 2016-11-01 09:44:59 |
| 4 | Work | Home | 2016-11-01 10:11:50 |
| 5 | Home | Work | 2016-11-01 10:25:30 |
| 6 | Work | Home | 2016-11-01 11:01:15 |
+----+-----------+-----------+---------------------+
我的查询:
SELECT loc1.*,loc2.date AS loc2_date, SUM(CAST(TIMESTAMPDIFF(MICROSECOND,loc1.date,loc2.date)/1000000 AS DECIMAL(20))) AS time_diff
FROM locations AS loc1
LEFT JOIN locations AS loc2 ON loc1.old_value=loc2.new_value AND loc2.id > loc1.id
WHERE old_value="Home"
我的查询结果:
Id: 1
Old Value: Home
New Value: Work
Date: 2016-11-01 09:00:00
Loc2_date: 2016-11-01 09:25:00
Time_fidd: 1500
Id: 1
Old Value: Home
New Value: Work
Date: 2016-11-01 09:00:00
Loc2_date: 2016-11-01 10:11:50
Time_diff: 4310
Id: 1
Old Value: Home
New Value: Work
Date: 2016-11-01 09:00:00
Loc2_date: 2016-11-01 11:01:15
Time_diff: 7275
Id: 3
Old Value: Home
New Value: Work
Date: 2016-11-01 09:44:59
Loc2_date: 2016-11-01 10:11:50
Time_diff: 1611
Id: 3
Old Value: Home
New Value: Work
Date: 2016-11-01 09:44:59
Loc2_date: 2016-11-01 11:01:15
Time_diff: 4576
Id: 5
Old Value: Home
New Value: Work
Date: 2016-11-01 10:25:30
Loc2_date: 2016-11-01 11:01:15
Time_diff: 2145
我想总结一下日期,看看我上班的所有时间。如果我按ID分组将准确显示我需要的内容,但当我总结loc1.date
和loc2.date
之间的差异时,查询将跳过分组依次并继续返回所有行。请帮忙。
我的查询需要什么:
Id: 1
Old Value: Home
New Value: Work
Date: 2016-11-01 09:00:00
Loc2_date: 2016-11-01 09:25:00
Time_fidd: 1500
Id: 3
Old Value: Home
New Value: Work
Date: 2016-11-01 09:44:59
Loc2_date: 2016-11-01 10:11:50
Time_diff: 1611
Id: 5
Old Value: Home
New Value: Work
Date: 2016-11-01 10:25:30
Loc2_date: 2016-11-01 11:01:15
Time_diff: 2145
答案 0 :(得分:1)
<强>更新强>
试试这个。
SELECT tablea.id, tablea.`Old_value`, tablea.`New_value`, tablea.`Date`, tablea.`Date` Loc2_date, SUM(tablea.time_diff) AS time_diff
FROM
(
SELECT loc1.`id`, loc1.`Old_value`, loc1.`New_value`, loc1.`Date`, loc2.`Date` Loc2_date, CAST(ABS(TIMESTAMPDIFF(MICROSECOND, loc2.date, loc1.date)/1000000) AS DECIMAL(20)) time_diff
FROM locations loc1 INNER JOIN locations loc2
ON loc1.`Old_value`=loc2.`New_value` AND loc2.`id`>loc1.`id`
WHERE loc1.`Old_value`="Home"
GROUP BY loc1.`id`
) tablea
示例:强>
SELECT tableb.*
FROM
(
SELECT tablea.id, tablea.`Old_value`, tablea.`New_value`, tablea.`Date`, tablea.`Date` Loc2_date, SUM(tablea.time_diff) AS time_diff
FROM
(
SELECT loc1.`id`, loc1.`Old_value`, loc1.`New_value`, loc1.`Date`, loc2.`Date` Loc2_date, CAST(ABS(TIMESTAMPDIFF(MICROSECOND, loc2.date, loc1.date)/1000000) AS DECIMAL(20)) time_diff
FROM locations loc1 INNER JOIN locations loc2
ON loc1.`Old_value`=loc2.`New_value` AND loc2.`id`>loc1.`id`
WHERE loc1.`Old_value`="Home"
GROUP BY loc1.`id`
) tablea
) tableb
旧答案。
您可以尝试 group_by 。在GROUP BY loc1.id
子句之后添加WHERE
。
SELECT loc1.*,loc2.date AS loc2_date, SUM(CAST(TIMESTAMPDIFF(MICROSECOND,loc1.date,loc2.date)/1000000 AS DECIMAL(20)))
FROM locations AS loc1
LEFT JOIN locations AS loc2 ON loc1.old_value=loc2.new_value AND loc2.id > loc1.id
WHERE old_value="Home"
GROUP BY loc1.id