34,837
社区成员




--建表
create table #Table (person varchar(5),ttime varchar(6),IOType int)
insert into #Table
select '001', '8:00', 1 union all
select '001', '12:00', 2 union all
select '001', '13:00', 1 union all
select '001', '17:00', 2 union all
select '002', '8:00', 1 union all
select '002', '12:00', 2
--换成时间格式
update #table Set ttime=substring(convert(char(21),cast(ttime as datetime),21),12,5)
--查询
select person,ttime as [in],(select min(ttime) from #Table where person=a.person and IoType=2 and ttime>a.ttime) as [out]
from #table as a
where Iotype=1
--结果
person in out
------ ------ ------
001 08:00 12:00
001 13:00 17:00
002 08:00 12:00
(所影响的行数为 3 行)
--建表
create table #Table (person varchar(5),ttime varchar(6),IOType int)
insert into #Table
select '001', '8:00', 1 union all
select '001', '12:00', 2 union all
select '001', '13:00', 1 union all
select '001', '17:00', 2 union all
select '002', '8:00', 1 union all
select '002', '12:00', 2
--换成时间格式
update #table Set ttime=substring(convert(char(21),cast(ttime as datetime),21),12,5)
--查询
select distinct * from
(
select person,
case when IoType=1 then ttime else (select max(ttime) from #table where person=a.person and IoType=1 and ttime<a.ttime) end as [in],
case when IoType=2 then ttime else (select min(ttime) from #Table where person=a.person and IoType=2 and ttime>a.ttime) end as [out]
from #table as a
) as a
--结果
person in out
------ ------ ------
001 08:00 12:00
001 13:00 17:00
002 08:00 12:00
(所影响的行数为 3 行)
DECLARE @T TABLE (person varchar(10), ttime varchar(10), iotype int)
insert into @t
select '001' ,'8:00',1 union all
select '001' ,'12:00',2 union all
select '001' ,'13:00',1 union all
select '001' ,'17:00',2 union all
select '002' ,'8:00',1 union all
select '002' ,'12:50',2
select person, inTime = ttime,
(
select min(b.ttime)
from @t as b
where 1=1
and b.person=a.person
and b.iotype=2
and b.person=a.person
and convert(datetime,b.ttime) > convert(datetime,a.ttime)
) as ttime2
from @t as a
where a.iotype=1
--sql server 2000中的写法.
create table tb (person varchar(10), ttime varchar(10),Iotype int)
insert into tb
select '001' ,'08:00',1 union all
select '001' ,'12:00',2 union all
select '001' ,'13:00',1 union all
select '001' ,'17:00',2 union all
select '002' ,'08:00',1 union all
select '002' ,'12:00',2
SELECT Wpid=(SELECT COUNT(1) FROM
(
select m.person , m.ttime [in], n.ttime [out] from
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) m,
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) n
where m.person = n.person and m.px = n.px - 1 and m.px%2 = 1
) t1 WHERE (person < t2.person) or (person = t2.person and [in] < t2.[in])) + 1 , * from
(
select m.person , m.ttime [in], n.ttime [out] from
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) m,
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) n
where m.person = n.person and m.px = n.px - 1 and m.px%2 = 1
) t2
drop table tb
/*
Wpid person in out
-------------------- ---------- ---------- ----------
1 001 08:00 12:00
2 001 13:00 17:00
3 002 08:00 12:00
(3 行受影响)
*/
--使用sql server 2005的row_number函数可以直接得到前面的序,sql server 2000要麻烦一些.
create table tb (person varchar(10), ttime varchar(10),Iotype int)
insert into tb
select '001' ,'08:00',1 union all
select '001' ,'12:00',2 union all
select '001' ,'13:00',1 union all
select '001' ,'17:00',2 union all
select '002' ,'08:00',1 union all
select '002' ,'12:00',2
select Wpid = row_number() over(order by m.person , m.ttime) , m.person , m.ttime [in], n.ttime [out] from
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) m,
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) n
where m.person = n.person and m.px = n.px - 1 and m.px%2 = 1
drop table tb
/*
Wpid person in out
-------------------- ---------- ---------- ----------
1 001 08:00 12:00
2 001 13:00 17:00
3 002 08:00 12:00
(3 行受影响)
*/
--我将数据略做修改,8:00改为08:00
create table tb (person varchar(10), ttime varchar(10),Iotype int)
insert into tb
select '001' ,'08:00',1 union all
select '001' ,'12:00',2 union all
select '001' ,'13:00',1 union all
select '001' ,'17:00',2 union all
select '002' ,'08:00',1 union all
select '002' ,'12:00',2
select m.person , m.ttime [in], n.ttime [out] from
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) m,
(select px = (select count(*) from tb where person = a.person and ttime < a.ttime) + 1 , * from tb a) n
where m.person = n.person and m.px = n.px - 1 and m.px%2 = 1
drop table tb
/*
person in out
---------- ---------- ----------
001 08:00 12:00
001 13:00 17:00
002 08:00 12:00
(3 行受影响)
*/