22,207
社区成员
发帖
与我相关
我的任务
分享
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[Date_Time] datetime,[Type] varchar(2))
insert [tb]
select 1,'2009-01-22 09:21','A1' union all
select 2,'2009-01-22 09:22','A1' union all
select 3,'2009-01-22 09:23','A1' union all
select 4,'2009-01-22 09:24','A1' union all
select 5,'2009-01-22 09:25','B1' union all
select 6,'2009-01-22 09:26','B1' union all
select 7,'2009-01-22 09:27','B1' union all
select 8,'2009-01-22 09:28','B1' union all
select 9,'2009-01-22 09:29','B1' union all
select 10,'2009-01-22 09:30','A1' union all
select 11,'2009-01-22 09:31','A1' union all
select 12,'2009-01-22 09:32','A1' union all
select 13,'2009-01-22 09:33','A1' union all
select 14,'2009-01-22 09:34','B1' union all
select 15,'2009-01-22 09:35','B1' union all
select 16,'2009-01-22 09:36','A1'
select [Type],
min(Date_Time) as Start_Time,
case count(1) when 1 then null else max(Date_Time) end as End_Time,
count(1)-1 as 连续次数
from
(select (select count(1) from tb where
Type=r.Type and Date_Time<=r.Date_Time)-id as group_id,*
from [tb] r) t
group by group_id,[Type]
--------------------------------
A1 2009-01-22 09:36:00.000 NULL 0
A1 2009-01-22 09:30:00.000 2009-01-22 09:33:00.000 3
A1 2009-01-22 09:21:00.000 2009-01-22 09:24:00.000 3
B1 2009-01-22 09:34:00.000 2009-01-22 09:35:00.000 1
B1 2009-01-22 09:25:00.000 2009-01-22 09:29:00.000 4
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
create table [tb]([id] int,[Date_Time] datetime,[Type] varchar(2))
insert [tb]
select 1,'2009-01-22 09:21','A1' union all
select 2,'2009-01-22 09:22','A1' union all
select 3,'2009-01-22 09:23','A1' union all
select 4,'2009-01-22 09:24','A1' union all
select 5,'2009-01-22 09:25','B1' union all
select 6,'2009-01-22 09:26','B1' union all
select 7,'2009-01-22 09:27','B1' union all
select 8,'2009-01-22 09:28','B1' union all
select 9,'2009-01-22 09:29','B1' union all
select 10,'2009-01-22 09:30','A1' union all
select 11,'2009-01-22 09:31','A1' union all
select 12,'2009-01-22 09:32','A1' union all
select 13,'2009-01-22 09:33','A1' union all
select 14,'2009-01-22 09:34','B1' union all
select 15,'2009-01-22 09:35','B1' union all
select 16,'2009-01-22 09:36','A1'
select [Type],
min(Date_Time) as Start_Time,
max(Date_Time) as End_Time,
count(1) as 连续次数
from
(select (select count(1) from tb where
Type=r.Type and Date_Time<=r.Date_Time)-id as group_id,*
from [tb] r) t
group by group_id,[Type]
------------------------------
A1 2009-01-22 09:36:00.000 2009-01-22 09:36:00.000 1
A1 2009-01-22 09:30:00.000 2009-01-22 09:33:00.000 4
A1 2009-01-22 09:21:00.000 2009-01-22 09:24:00.000 4
B1 2009-01-22 09:34:00.000 2009-01-22 09:35:00.000 2
B1 2009-01-22 09:25:00.000 2009-01-22 09:29:00.000 5
---查询---
select px=identity(int,1,1),* into #1
from tb t
where not exists(select 1 from tb where type=t.type and datediff(mi,date_time,t.date_time)=1)
go
select px=identity(int,1,1),* into #2
from tb t
where not exists(select 1 from tb where type=t.type and datediff(mi,date_time,t.date_time)=-1)
go
select a.type,
Start_Time=a.Date_Time,
End_Time=case when b.Date_Time=a.Date_Time then null else b.Date_Time end,
datediff(mi,a.Date_Time,b.Date_Time) as 连续个数
from #1 a
left join #2 b
on a.type=b.type and a.px=b.px
/**
type Start_Time End_Time 连续个数
---- ------------------------------------------------------ ------------------------------------------------------ -----------
A1 2009-01-22 09:21:00.000 2009-01-22 09:24:00.000 3
B1 2009-01-22 09:25:00.000 2009-01-22 09:29:00.000 4
A1 2009-01-22 09:30:00.000 2009-01-22 09:33:00.000 3
B1 2009-01-22 09:34:00.000 2009-01-22 09:35:00.000 1
A1 2009-01-22 09:36:00.000 NULL 0
(所影响的行数为 5 行)
**/
---测试数据---
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[Date_Time] datetime,[Type] varchar(2))
insert [tb]
select 1,'2009-01-22 09:21','A1' union all
select 2,'2009-01-22 09:22','A1' union all
select 3,'2009-01-22 09:23','A1' union all
select 4,'2009-01-22 09:24','A1' union all
select 5,'2009-01-22 09:25','B1' union all
select 6,'2009-01-22 09:26','B1' union all
select 7,'2009-01-22 09:27','B1' union all
select 8,'2009-01-22 09:28','B1' union all
select 9,'2009-01-22 09:29','B1' union all
select 10,'2009-01-22 09:30','A1' union all
select 11,'2009-01-22 09:31','A1' union all
select 12,'2009-01-22 09:32','A1' union all
select 13,'2009-01-22 09:33','A1' union all
select 14,'2009-01-22 09:34','B1' union all
select 15,'2009-01-22 09:35','B1' union all
select 16,'2009-01-22 09:36','A1'
---查询---
select px=identity(int,1,1),* into #1
from tb t
where not exists(select 1 from tb where type=t.type and datediff(mi,date_time,t.date_time)=1)
go
select px=identity(int,1,1),* into #2
from tb t
where not exists(select 1 from tb where type=t.type and datediff(mi,date_time,t.date_time)=-1)
go
select a.type,
Start_Time=a.Date_Time,
End_Time=case when b.Date_Time=a.Date_Time then null else b.Date_Time end
from #1 a
left join #2 b
on a.type=b.type and a.px=b.px
---结果---
type Start_Time End_Time
---- ------------------------------------------------------ ------------------------------------------------------
A1 2009-01-22 09:21:00.000 2009-01-22 09:24:00.000
B1 2009-01-22 09:25:00.000 2009-01-22 09:29:00.000
A1 2009-01-22 09:30:00.000 2009-01-22 09:33:00.000
B1 2009-01-22 09:34:00.000 2009-01-22 09:35:00.000
A1 2009-01-22 09:36:00.000 NULL
(所影响的行数为 5 行)
--先这样 剩下的就简单了吧 呵呵
----------------------------------------------------------------
-- Author :fredrickhu(我是小F,向高手学习)
-- Date :2010-01-22 10:59:42
-- Version:
-- Microsoft SQL Server 2005 - 9.00.4035.00 (Intel X86)
-- Nov 24 2008 13:01:59
-- Copyright (c) 1988-2005 Microsoft Corporation
-- Developer Edition on Windows NT 5.2 (Build 3790: Service Pack 1)
--
----------------------------------------------------------------
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([id] int,[Date_Time] datetime,[Type] varchar(2))
insert [tb]
select 1,'2009-01-22 09:21','A1' union all
select 2,'2009-01-22 09:22','A1' union all
select 3,'2009-01-22 09:23','A1' union all
select 4,'2009-01-22 09:24','A1' union all
select 5,'2009-01-22 09:25','B1' union all
select 6,'2009-01-22 09:26','B1' union all
select 7,'2009-01-22 09:27','B1' union all
select 8,'2009-01-22 09:28','B1' union all
select 9,'2009-01-22 09:29','B1' union all
select 10,'2009-01-22 09:30','A1' union all
select 11,'2009-01-22 09:31','A1' union all
select 12,'2009-01-22 09:32','A1' union all
select 13,'2009-01-22 09:33','A1' union all
select 14,'2009-01-22 09:34','B1' union all
select 15,'2009-01-22 09:35','B1' union all
select 16,'2009-01-22 09:36','A1'
--------------开始查询--------------------------
select id0=identity(int,1,1),* into # from tb
select
id,date_time,type,px=(select
count(1)
from
#
where
[Type]=a.[Type] and id0<=a.id0
and
id>=(select isnull(max(id0),0) from # where id0<a.id0 and [Type]!=a.[Type]))
from
# a
drop table #
----------------结果----------------------------
/* id date_time type px
----------- ----------------------- ---- -----------
1 2009-01-22 09:21:00.000 A1 1
2 2009-01-22 09:22:00.000 A1 2
3 2009-01-22 09:23:00.000 A1 3
4 2009-01-22 09:24:00.000 A1 4
5 2009-01-22 09:25:00.000 B1 1
6 2009-01-22 09:26:00.000 B1 2
7 2009-01-22 09:27:00.000 B1 3
8 2009-01-22 09:28:00.000 B1 4
9 2009-01-22 09:29:00.000 B1 5
10 2009-01-22 09:30:00.000 A1 1
11 2009-01-22 09:31:00.000 A1 2
12 2009-01-22 09:32:00.000 A1 3
13 2009-01-22 09:33:00.000 A1 4
14 2009-01-22 09:34:00.000 B1 1
15 2009-01-22 09:35:00.000 B1 2
16 2009-01-22 09:36:00.000 A1 1
(16 行受影响)
*/
select type,
min(Date_Time) as Start_Time,
max(Date_Time) as End_Time
from tb
group by type