[英]Mysql query - one table - find two dates within one day - of the same entity from different column + diff calculations
I'm trying to figure a mysql query to do calculations of a table containing ins and outs of people in the office. 我正在尝试构建一个mysql查询来计算包含办公室人员来龙去脉的表格。
What I have: 我有的:
id PERSON IN OUT
1 Person A 2019-03-11 08:59:30 NULL
2 Person B 2019-03-11 08:32:00 NULL
3 Person C 2019-03-11 08:04:40 NULL
4 Person D 2019-03-11 07:58:50 NULL
5 Person E 2019-03-11 07:35:20 NULL
6 Person F 2019-03-11 07:35:00 NULL
7 Person A NULL 2019-03-11 15:00:50
8 Person B NULL 2019-03-11 14:57:00
8 Person C NULL 2019-03-11 13:19:50
9 Person D NULL 2019-03-11 15:14:20
10 Person E NULL 2019-03-11 15:15:50
11 Person F NULL 2019-03-11 15:28:10
What I would like to get: 我想得到什么:
id PERSON IN OUT DIFF IN MINUTES
1 Person A 2019-03-11 08:59:30 2019-03-11 15:00:50 XXX
2 Person B 2019-03-11 08:32:00 2019-03-11 14:57:00 XXX
3 Person C 2019-03-11 08:04:40 2019-03-11 13:19:50 XXX
4 Person D 2019-03-11 07:58:50 2019-03-11 15:14:20 XXX
5 Person E 2019-03-11 07:35:20 2019-03-11 15:15:50 XXX
6 Person F 2019-03-11 07:35:00 2019-03-11 15:28:10 XXX
TOTAL OF XXXS
TOTAL OF XXXS - YYY (constant)
The idea is to get the information of the time spent in the office during one day. 这个想法是获取在一天内在办公室花费的时间的信息。 Moreover I need summary of minutes from the whole month per person.
此外,我需要每人整个月的会议记录摘要。 Grouping per person/per month.
按人/每月分组。
I have spent some time and I use this query, but the effect is mediocre: 我花了一些时间来使用这个查询,但效果是平庸的:
SELECT a.PERSON, a.IN, b.OUT, TIMESTAMPDIFF(SECOND,a.IN,b.OUT)-28880 AS WHATSLEFT
FROM presence a
INNER JOIN presence b
ON a.PERSON = b.PERSON
WHERE DATEDIFF(a.IN,b.OUT) = 0 AND b.PERSON ="Person A"
ORDER BY a.IN;
Thanks for help! 感谢帮助! Adam
亚当
You can start from here: 你可以从这里开始:
SELECT a.PERSON, a.IN, b.OUT, TIMESTAMPDIFF(SECOND, a.IN, b.OUT) AS TOTAL
FROM presence a
LEFT JOIN presence b ON b.id = (
SELECT MIN(b2.id)
FROM presence b2
WHERE b2.PERSON = a.person
AND b2.id > a.id
AND b2.OUT IS NOT NULL
)
WHERE a.IN IS NOT NULL
ORDER BY a.IN;
It will return: 它将返回:
PERSON IN OUT TOTAL
-----------------------------------------------------------------
Person F 2019-03-11 07:35:00 2019-03-11 15:28:10 28390
Person E 2019-03-11 07:35:20 2019-03-11 15:15:50 27630
Person D 2019-03-11 07:58:50 2019-03-11 15:14:20 26130
Person C 2019-03-11 08:04:40 2019-03-11 13:19:50 18910
Person B 2019-03-11 08:32:00 2019-03-11 14:57:00 23100
Person A 2019-03-11 08:59:30 2019-03-11 15:00:50 21680
The query joins a row which has a value in IN
with the next row from the same person which has a value in OUT
. 该查询将具有
IN
值的行与来自具有OUT
值的同一个人的下一行连接。 This assumes that id
is an AUTO_INCREMENT PRIMARY KEY and your data is correct. 这假设
id
是AUTO_INCREMENT PRIMARY KEY并且您的数据是正确的。
You can now change it to a GROUP BY query. 您现在可以将其更改为GROUP BY查询。
An improvement of the query provided by paul for the minutes each day would be: 保罗每天为分钟提供的查询的改进将是:
SELECT
a.PERSON,
a.IN,
b.OUT,
TIMESTAMPDIFF(MINUTE,a.IN,b.OUT) AS `DIFF IN MINUTES`
FROM presence a
INNER JOIN presence b ON ( a.PERSON = b.PERSON )
WHERE DAY(a.IN) = DAY(b.OUT)
ORDER BY a.IN;
The query for the moth would look like this: 蛾的查询如下所示:
SELECT
a.PERSON, SUM( TIMESTAMPDIFF(MINUTE,a.IN,b.OUT) ) AS `MINUTES`
FROM presence a
INNER JOIN presence b ON ( a.PERSON = b.PERSON )
WHERE DAY(a.IN) = DAY(b.OUT)
AND a.PERSON = 'Person A'
AND a.IN BETWEEN '2019-03-01' AND '2019-04-01'
AND b.OUT BETWEEN '2019-03-01' AND '2019-04-01'
GROUP BY a.PERSON;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.