34,838
社区成员




if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名称] varchar(5),[时间] datetime,[状态] varchar(4))
insert [tb]
select '汽车A','2001-1-1 1:1:1','出发' union all
select '汽车A','2001-1-1 2:2:2','返回' union all
select '汽车B','2001-1-1 3:3:3','出发' union all
select '汽车C','2001-1-1 5:5:5','出发' union all
select '汽车B','2001-1-1 4:4:4','返回' union all
select '汽车C','2001-1-1 6:6:6','返回' union all
select '汽车D','2001-1-1 7:7:7','出发' union all
select '汽车A','2001-1-1 8:8:8','出发' union all
select '汽车A','2001-1-1 9:9:9','返回'
select 名称,min(case when 状态='出发' then [时间] else null end) as 出发时间,
max(case when 状态='返回' then [时间] else null end) as 返回时间
from (select 2*(select count(*) from tb where 名称=a.名称 and 时间<a.时间 and 状态='返回') as times,* from tb a) t
group by 名称,times
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名称] varchar(5),[时间] datetime,[状态] varchar(4))
insert [tb]
select '汽车A','2001-1-1 2:2:2','返回' union all
select '汽车B','2001-1-1 3:3:3','出发' union all
select '汽车C','2001-1-1 5:5:5','出发' union all
select '汽车B','2001-1-1 4:4:4','返回' union all
select '汽车C','2001-1-1 6:6:6','返回' union all
select '汽车D','2001-1-1 7:7:7','出发' union all
select '汽车A','2001-1-1 8:8:8','出发' union all
select '汽车A','2001-1-1 9:9:9','返回' union all
select '汽车A','2001-1-1 11:11:11','出发' union all
select '汽车A','2001-1-1 12:12:12','返回'
---查询---
select
isnull(a.[名称],b.[名称]) as 名称,
a.[时间] as [出发时间],
b.[时间] as [返回时间]
from
(
select
[名称],
[时间],
px=(case when (select min(时间) from tb where [状态]='返回' and [名称]=t.[名称])<t.[时间] then 1 else 0 end)
+(select count(1)+1 from tb where [状态]='出发' and [名称]=t.[名称] and [时间]<t.[时间])
from
tb t
where
[状态]='出发'
) a
full join
(
select
[名称],
[时间],
px=(select count(1)+1 from tb where [状态]='返回' and [名称]=t.[名称] and [时间]<t.[时间])
from
tb t
where
[状态]='返回'
) b
on
a.[名称]=b.[名称] and a.px=b.px
/**
名称 出发时间 返回时间
----- ------------------------------------------------------ ------------------------------------------------------
汽车B 2001-01-01 03:03:03.000 2001-01-01 04:04:04.000
汽车C 2001-01-01 05:05:05.000 2001-01-01 06:06:06.000
汽车D 2001-01-01 07:07:07.000 NULL
汽车A 2001-01-01 08:08:08.000 2001-01-01 09:09:09.000
汽车A 2001-01-01 11:11:11.000 2001-01-01 12:12:12.000
汽车A NULL 2001-01-01 02:02:02.000
(所影响的行数为 6 行)
**/
---查询---
select
isnull(a.[名称],b.[名称]) as 名称,
a.[时间] as [出发时间],
b.[时间] as [返回时间]
from
(select [名称],[时间] from tb t where [状态]='出发') a
full join
(select [名称],[时间] from tb t where [状态]='返回') b
on
a.[名称]=b.[名称] and a.时间<b.时间
---结果---
名名称 出发时间 返回时间
----- ------------------------------------------------------ ------------------------------------------------------
汽车A NULL 2001-01-01 02:02:02.000
汽车B 2001-01-01 03:03:03.000 2001-01-01 04:04:04.000
汽车C 2001-01-01 05:05:05.000 2001-01-01 06:06:06.000
汽车A 2001-01-01 08:08:08.000 2001-01-01 09:09:09.000
汽车D 2001-01-01 07:07:07.000 NULL
(所影响的行数为 5 行)
select 名称,出发时间=时间,返回时间=(select top 1 时间 where 时间>a.时间and 名称=a.名称) from tb a where a.状态='出发'
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([名称] varchar(5),[时间] datetime,[状态] varchar(4))
insert [tb]
select '汽车A','2001-1-1 2:2:2','返回' union all
select '汽车B','2001-1-1 3:3:3','出发' union all
select '汽车C','2001-1-1 5:5:5','出发' union all
select '汽车B','2001-1-1 4:4:4','返回' union all
select '汽车C','2001-1-1 6:6:6','返回' union all
select '汽车D','2001-1-1 7:7:7','出发' union all
select '汽车A','2001-1-1 8:8:8','出发' union all
select '汽车A','2001-1-1 9:9:9','返回'
---查询---
select
isnull(a.[名称],b.[名称]) as 名称,
a.[时间] as [出发时间],
b.[时间] as [返回时间]
from
(select [名称],[时间],px=(select count(1)+1 from tb where [状态]='出发' and [名称]=t.[名称] and [时间]<t.[时间]) from tb t where [状态]='出发') a
full join
(select [名称],[时间],px=(select count(1)+1 from tb where [状态]='返回' and [名称]=t.[名称] and [时间]<t.[时间]) from tb t where [状态]='返回') b
on
a.[名称]=b.[名称] and a.px=b.px
---结果---
名称 出发时间 返回时间
----- ------------------------------------------------------ ------------------------------------------------------
汽车A 2001-01-01 08:08:08.000 2001-01-01 02:02:02.000
汽车B 2001-01-01 03:03:03.000 2001-01-01 04:04:04.000
汽车C 2001-01-01 05:05:05.000 2001-01-01 06:06:06.000
汽车A NULL 2001-01-01 09:09:09.000
汽车D 2001-01-01 07:07:07.000 NULL
(所影响的行数为 5 行)
-->生成测试数据
declare @Tab table(
[名称] varchar(10), [时间] datetime, [状态] varchar(10))
insert into @Tab select '汽车A', '2001-1-1 1:1:1', '出发'
insert into @Tab select '汽车A', '2001-1-1 2:2:2', '返回'
insert into @Tab select '汽车B', '2001-1-1 3:3:3', '出发'
insert into @Tab select '汽车C', '2001-1-1 5:5:5', '出发'
insert into @Tab select '汽车B', '2001-1-1 4:4:4', '返回'
insert into @Tab select '汽车C', '2001-1-1 6:6:6', '返回'
insert into @Tab select '汽车D', '2001-1-1 7:7:7', '出发'
insert into @Tab select '汽车A', '2001-1-1 8:8:8', '出发'
insert into @Tab select '汽车A', '2001-1-1 9:9:9', '返回'
-->生成测试环境
;WITH Args AS (
select a.[名称],
a.[时间] as [出发时间],
b.[时间] as [返回时间]
from
(select [名称],[时间],ROW_NUMBER() OVER (PARTITION BY [名称] ORDER BY [名称]) Num from @Tab where [状态]='出发'
) a
left join
(select [名称],[时间],ROW_NUMBER() OVER (PARTITION BY [名称] ORDER BY [名称]) Num from @Tab where [状态]='返回'
) b
on a.[名称]=b.[名称] AND a.Num = b.Num)
SELECT * FROM Args ORDER BY [出发时间]
-->测试结果
名称 出发时间 返回时间
---------- ----------------------- -----------------------
汽车A 2001-01-01 01:01:01.000 2001-01-01 02:02:02.000
汽车B 2001-01-01 03:03:03.000 2001-01-01 04:04:04.000
汽车C 2001-01-01 05:05:05.000 2001-01-01 06:06:06.000
汽车D 2001-01-01 07:07:07.000 NULL
汽车A 2001-01-01 08:08:08.000 2001-01-01 09:09:09.000
(5 行受影响)
-->生成测试数据
declare @Tab table(
[名称] varchar(10), [时间] datetime, [状态] varchar(10))
insert into @Tab select '汽车A', '2001-1-1 1:1:1', '出发'
insert into @Tab select '汽车A', '2001-1-1 2:2:2', '返回'
insert into @Tab select '汽车B', '2001-1-1 3:3:3', '出发'
insert into @Tab select '汽车C', '2001-1-1 5:5:5', '出发'
insert into @Tab select '汽车B', '2001-1-1 4:4:4', '返回'
insert into @Tab select '汽车C', '2001-1-1 6:6:6', '返回'
insert into @Tab select '汽车D', '2001-1-1 7:7:7', '出发'
insert into @Tab select '汽车A', '2001-1-1 8:8:8', '出发'
insert into @Tab select '汽车A', '2001-1-1 9:9:9', '返回'
-->生成测试环境
select a.[名称],
a.[时间] as [出发时间],
b.[时间] as [返回时间]
from
(select [名称],[时间],ROW_NUMBER() OVER (PARTITION BY [名称] ORDER BY [名称]) Num from @Tab where [状态]='出发'
) a
left join
(select [名称],[时间],ROW_NUMBER() OVER (PARTITION BY [名称] ORDER BY [名称]) Num from @Tab where [状态]='返回'
) b
on a.[名称]=b.[名称] AND a.Num = b.Num
-->测试结果
名称 出发时间 返回时间
---------- ----------------------- -----------------------
汽车A 2001-01-01 01:01:01.000 2001-01-01 02:02:02.000
汽车A 2001-01-01 08:08:08.000 2001-01-01 09:09:09.000
汽车B 2001-01-01 03:03:03.000 2001-01-01 04:04:04.000
汽车C 2001-01-01 05:05:05.000 2001-01-01 06:06:06.000
汽车D 2001-01-01 07:07:07.000 NULL