27,579
社区成员
发帖
与我相关
我的任务
分享
create table t1
(
address1 varchar(10),
starttime datetime
)
insert into t1
select 'aa', '2010-1-2' union all
select 'aa', '2010-1-3' union all
select 'aa', '2010-1-4' union all
select 'bb', '2010-1-5' union all
select 'bb', '2010-1-6' union all
select 'cc', '2010-1-7' union all
select 'aa', '2010-1-10' union all
select 'aa', '2010-1-11'
select * from t1
;with aaa as
(select ROW_NUMBER() over(order by (select 0)) as row,* from t1)
,bbb as
(select *,DATEADD(DAY,-row,starttime) as endtime from aaa)
select address1,MIN(starttime) as starttime,MAX(starttime) as endtime from bbb group by address1,endtime
select address,mintime=min(startime),maxtime=max(startime)
from tb group by address,left(startime,6),len(startime)
学习,占位,收藏,学习!
IF OBJECT_ID('tempdb..#t', 'u') IS NOT NULL
DROP TABLE #t
go
create table #t(address varchar(10),startime DATETIME)
insert into #t(address, startime)
select
'aa', '2010-1-2' union all select
'aa', '2010-1-3' union all select
'aa', '2010-1-4' union all select
'bb', '2010-1-5' union all select
'bb', '2010-1-6' union all select
'cc', '2010-1-7' union all select
'aa', '2010-1-10' union all select
'aa', '2010-1-11'
GO
--SQL:
;WITH cte AS
(
SELECT
rowno = ROW_NUMBER() OVER(ORDER BY startime),
groupno = ROW_NUMBER() OVER(PARTITION BY [address] ORDER BY startime),
*
FROM #t
)
SELECT
[address],
mintime=CONVERT(CHAR(10), MIN(startime), 120),
maxtime=CONVERT(CHAR(10), MAX(startime), 120)
FROM cte
GROUP BY [address], rowno-groupno
ORDER BY MIN(startime)
/*
aa 2010-01-02 2010-01-04
bb 2010-01-05 2010-01-06
cc 2010-01-07 2010-01-07
aa 2010-01-10 2010-01-11
*/
create table #tb
(address nvarchar(10), startime nvarchar(10))
insert #tb
select 'aa' ,'2010-1-2' union all
select 'aa' ,'2010-1-3' union all
select 'aa' ,'2010-1-4' union all
select 'bb' ,'2010-1-5' union all
select 'bb' ,'2010-1-6' union all
select 'cc' ,'2010-1-7' union all
select 'aa' ,'2010-1-10' union all
select 'aa' ,'2010-1-11'
;with T as
(select year(startime)+month(startime)+day(startime)-(Row_Number() over(partition by address order by cast(startime as datetime))) gid
,* from #tb)
select address,min(startime) as mintime,max(startime) as maxtime from T group by address ,gid
create table tb(address varchar(100), startime datetime)
insert tb
select 'aa','2010-1-2' union
select 'aa','2010-1-3' union
select 'aa','2010-1-4' union
select 'bb','2010-1-5' union
select 'bb','2010-1-6' union
select 'cc','2010-1-7'
insert tb
select 'aa','2010-1-10' union
select 'aa','2010-1-11'
alter table tb add lb int
go
declare @var varchar(100),@n int
set @var='aa';set @n=1
update tb
set @n=case when @var=address then @n else @n+1 end,
@var=address,
lb=@n
go
select address=max(address),
mintime=min(startime),
maxtime=max(startime)
from tb group by lb
go
drop table tb
create table #t(address varchar(10),startime varchar(50))
insert into #t(address, startime)
select
'aa', '2010-1-2' union select
'aa', '2010-1-3' union select
'aa', '2010-1-4' union select
'bb', '2010-1-5' union select
'bb', '2010-1-6' union select
'cc', '2010-1-7' union select
'aa', '2010-1-10' union select
'aa', '2010-1-11'
--插入临时表#tb
select identity(int,1,1)as id, 0 as num , address, cast(startime as datetime) startime into #tb from #t order by cast(startime as datetime)
--更新临时表num字段
declare @num int
set @num =1
update #tb set num = @num ,
@num = (case when (select address from #tb where id =a.id-1) <>a.address then @num+1 else @num end)
from #tb a
--查询所要的结果集
select address, min(startime) as mintime,max(startime) as maxtime from #tb group by num , address
order by mintime
--释放临时表
drop table #t
drop table #tb
/*
address mintime maxtime
---------- ----------------------- -----------------------
aa 2010-01-02 00:00:00.000 2010-01-04 00:00:00.000
bb 2010-01-05 00:00:00.000 2010-01-06 00:00:00.000
cc 2010-01-07 00:00:00.000 2010-01-07 00:00:00.000
aa 2010-01-10 00:00:00.000 2010-01-11 00:00:00.000
(4 row(s) affected)