请参阅此链接:http://sqlfiddle.com/#!9/f435e/15。
我希望在MS SQL Server上运行此查询。这目前正在我的MySQL上运行,但不幸的是我们正在迁移到MS SQL Server。
请帮忙。
答案 0 :(得分:0)
create table logs (Pin varchar(20),Mode varchar(100),Date_time datetime(3));
insert into logs values ('16514','IN','2015-06-12 16:37:46');
insert into logs values ('16514','OUT','2015-06-13 06:37:46');
insert into logs values ('16514','IN','2015-06-13 16:37:46');
insert into logs values ('16514','OUT','2015-06-14 06:37:46');
select pin, in_datetime,
out_datetime,
date_format(SEC_TO_TIME(TIMESTAMPDIFF(SECOND,in_datetime,out_datetime)),
'%H:%i:%S') as duration
from (
select
pin
, date_time as in_datetime
, (select l.date_time from logs as l
where l.date_time > logs.date_time
and l.mode = 'OUT'
order by l.date_time ASC
LIMIT 1) as out_datetime
from logs
where mode = 'IN'
) as
答案 1 :(得分:0)
好的,创建和插入查询没问题。
但是对于select查询,您可以在SQL SERVER中尝试以下查询:
select
pin
, in_datetime
, out_datetime
, CONVERT(varchar, DATEADD(ms, DATEDIFF(SECOND,in_datetime,out_datetime) * 1000, 0), 114) as duration
from (
select
pin
, date_time as in_datetime
, (select TOP 1 l.date_time from logs as l
where l.date_time > logs.date_time
and l.mode = 'OUT'
order by l.date_time ASC) as out_datetime
from logs
where mode = 'IN'
) as relog