问题描述
我正在尝试找到一种方法来确定表中两个独立行之间的时间量。
例如,我的表有用户,日期和时间戳记,交易类型
数据示例:
Joe 10/2/2020 9:01:30 Shipping
Joe 10/2/2020 9:01:55 Shipping
Joe 10/02/2020 9:05:30 Shipping
我想在输出中看到
user date/time time difference
Joe 10/2/2020 9:01:30
Joe 10/2/2020 9:01:55 25 seconds
Joe 10/02/2020 9:05:30 3 min 35 seconds
最终目标是拥有不同的交易类型,并找出用户在每种交易类型下工作多长时间,而我只有结束时间才能工作。
解决方法
对于MYSQL Server版本8 +
如果您正在使用MySQL Server的最新版本(例如8.0版),则可以尝试使用 LAG()函数。
样品表:
CREATE TABLE WorkLog (
employee_name varchar(10),Shipping_Time DATETIME,PRIMARY KEY (employee_name,Shipping_Time)
);
样本数据:
INSERT INTO WorkLog(employee_name,Shipping_Time) VALUES("Joe","2020-10-02 12:00:00.000");
INSERT INTO WorkLog(employee_name,"2020-10-02 12:01:30.000");
INSERT INTO WorkLog(employee_name,"2020-10-02 12:05:30.000");
INSERT INTO WorkLog(employee_name,"2020-10-02 12:10:00.000");
INSERT INTO WorkLog(employee_name,"2020-10-02 12:20:00.000");
INSERT INTO WorkLog(employee_name,Shipping_Time) VALUES("Sue","2020-10-02 12:20:00.000");
SQL查询-以秒为单位获取结果:
SELECT employee_name,Shipping_Time,TIMESTAMPDIFF(SECOND,(LAG(Shipping_Time,1) OVER (PARTITION BY employee_name)),Shipping_Time) AS TimeDiffInSeconds
FROM WorkLog
ORDER BY employee_name,Shipping_Time;
输出:
| employee_name | Shipping_Time | TimeDiffInSeconds |
| ------------- | ------------------- | ----------------- |
| Joe | 2020-10-02 12:00:00 | |
| Joe | 2020-10-02 12:01:30 | 90 |
| Joe | 2020-10-02 12:05:30 | 240 |
| Joe | 2020-10-02 12:10:00 | 270 |
| Joe | 2020-10-02 12:20:00 | 600 |
| Sue | 2020-10-02 12:00:00 | |
| Sue | 2020-10-02 12:01:30 | 90 |
| Sue | 2020-10-02 12:05:30 | 240 |
| Sue | 2020-10-02 12:10:00 | 270 |
| Sue | 2020-10-02 12:20:00 | 600 |
SQL查询-以时间(hh:mm:ss)的形式获取结果:
SELECT employee_name,SEC_TO_TIME(TIMESTAMPDIFF(SECOND,Shipping_Time)) AS "TimeDiff hh:mm:ss"
FROM WorkLog
ORDER BY employee_name,Shipping_Time;
输出:
| employee_name | Shipping_Time | TimeDiff hh:mm:ss |
| ------------- | ------------------- | ----------------- |
| Joe | 2020-10-02 12:00:00 | |
| Joe | 2020-10-02 12:01:30 | 00:01:30 |
| Joe | 2020-10-02 12:05:30 | 00:04:00 |
| Joe | 2020-10-02 12:10:00 | 00:04:30 |
| Joe | 2020-10-02 12:20:00 | 00:10:00 |
| Sue | 2020-10-02 12:00:00 | |
| Sue | 2020-10-02 12:01:30 | 00:01:30 |
| Sue | 2020-10-02 12:05:30 | 00:04:00 |
| Sue | 2020-10-02 12:10:00 | 00:04:30 |
| Sue | 2020-10-02 12:20:00 | 00:10:00 |
参考:https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html#function_lag
对于MS SQL Server
使用OP评论中的新信息:
样品表:
CREATE TABLE JSSHIPMENTTABLE (
CREATEDBY varchar(10),CREATEDDATETIME DATETIME,PRIMARY KEY (CREATEDBY,CREATEDDATETIME)
);
样本数据:
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,CREATEDDATETIME) VALUES('Joe','2020-10-02 12:00:00.000');
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,'2020-10-02 12:01:30.000');
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,CREATEDDATETIME) VALUES('Sue',CREATEDDATETIME) VALUES('Sam Colton','2020-10-01 12:00:00.000');
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,'2020-10-01 12:01:30.000');
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,'2020-10-01 12:05:30.000');
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,'2020-10-01 12:10:00.000');
INSERT INTO JSSHIPMENTTABLE(CREATEDBY,'2020-10-01 12:20:00.000');
MS SQL Server查询-以秒为单位获取结果:
SELECT CREATEDBY,CREATEDDATETIME,datediff(second,(lag(CREATEDDATETIME,1) over (PARTITION by CREATEDBY ORDER BY CREATEDDATETIME)),CREATEDDATETIME) as TimeDiffinSeconds
from JSSHIPMENTTABLE
where convert(date,23) = '2020-10-01' and CREATEDBY like '%Colton%' order by CREATEDBY,CREATEDDATETIME
输出:
CREATEDBY CREATEDDATETIME TimeDiffinSeconds
----------------------------------------------------------
Sam Colton 2020-10-01 12:00:00.000 NULL
Sam Colton 2020-10-01 12:01:30.000 90
Sam Colton 2020-10-01 12:05:30.000 240
Sam Colton 2020-10-01 12:10:00.000 270
Sam Colton 2020-10-01 12:20:00.000 600
,
尝试此查询。您可以编辑DATEDIFF函数,以分钟,小时,天或任何所需的增量为您提供差异。
SELECT a.user,a.[date/time],DATEDIFF(second,b.[date/time],a.[date/time]) AS [time difference]
FROM #YourTable a
OUTER APPLY (
SELECT TOP 1 *
FROM #YourTable b
WHERE a.user = b.user
AND b.[date/time] < a.[date/time]
ORDER BY [date/time] DESC
) b
ORDER BY a.user,a.[date/time] ASC
,
-- Sample Table:
CREATE TABLE WorkLog (
employee_name varchar(10),Shipping_Time)
);
-- Sample Data:
INSERT INTO WorkLog(employee_name,Shipping_Time) VALUES('Joe','2020-10-02 12:00:00.000');
INSERT INTO WorkLog(employee_name,'2020-10-02 12:01:30.000');
INSERT INTO WorkLog(employee_name,'2020-10-02 12:05:30.000');
INSERT INTO WorkLog(employee_name,'2020-10-02 12:10:00.000');
INSERT INTO WorkLog(employee_name,'2020-10-02 12:20:00.000');
Select *,FORMAT(Shipping_Time - LAG(Shipping_Time) OVER (ORDER BY employee_name),'mm:ss') As timeDiference
From WorkLog
Order By Shipping_Time ASC
-- Results:
employee_name Shipping_Time timeDiference
Joe 2020-10-02 12:00:00.000 NULL
Joe 2020-10-02 12:01:30.000 01:30
Joe 2020-10-02 12:05:30.000 04:00
Joe 2020-10-02 12:10:00.000 04:30
Joe 2020-10-02 12:20:00.000 10:00