34,588
社区成员
发帖
与我相关
我的任务
分享
create table #t(id int identity(1,1),dt datetime )
insert into #t(dt)
select '2011-08-01'
union all
select '2011-08-02'
union all
select '2011-08-02'
union all
select '2011-08-03'
union all
select '2011-08-04'
union all
select '2011-08-05'
union all
select '2011-08-06'
union all
select '2011-08-07'
union all
select '2011-08-08'
union all
select '2011-08-08'
union all
select '2011-08-09'
union all
select '2011-08-10'
union all
select '2011-08-12'
union all
select '2011-08-13'
union all
select '2011-08-14'
union all
select '2011-08-15'
union all
select '2011-08-16'
union all
select '2011-08-17'
union all
select '2011-08-18'
union all
select '2011-08-19'
union all
select '2011-08-20'
union all
select '2011-08-21'
;with cte as (
select dr=dense_rank() over(order by a.dt),a.*,DATEDIFF(dd,'2011-01-01',a.dt) as x from #t a
)
select rn=(dense_rank() over(partition by X-dr order by dr)-1)%8+1,a.id,a.dt from cte a;
/*
rn id dt
-------------------- ----------- -----------------------
1 1 2011-08-01 00:00:00.000
2 2 2011-08-02 00:00:00.000
2 3 2011-08-02 00:00:00.000
3 4 2011-08-03 00:00:00.000
4 5 2011-08-04 00:00:00.000
5 6 2011-08-05 00:00:00.000
6 7 2011-08-06 00:00:00.000
7 8 2011-08-07 00:00:00.000
8 9 2011-08-08 00:00:00.000
8 10 2011-08-08 00:00:00.000
1 11 2011-08-09 00:00:00.000
2 12 2011-08-10 00:00:00.000
1 13 2011-08-12 00:00:00.000
2 14 2011-08-13 00:00:00.000
3 15 2011-08-14 00:00:00.000
4 16 2011-08-15 00:00:00.000
5 17 2011-08-16 00:00:00.000
6 18 2011-08-17 00:00:00.000
7 19 2011-08-18 00:00:00.000
8 20 2011-08-19 00:00:00.000
1 21 2011-08-20 00:00:00.000
2 22 2011-08-21 00:00:00.000
*/
;with cte as (
select dr=dense_rank() over(order by a.dt),a.*,DATEDIFF(dd,'2011-01-01',a.dt) as x from #t a left join #t b on a.id=b.id-1
)
select rn=(dense_rank() over(partition by X-dr order by dr)-1)%8+1,a.id,a.dt from cte a;
-- 参考上面小三的代码
-- 楼主是否是游标哥?
--2005
create table #t(id int identity(1,1),dt datetime )
insert into #t(dt)
select '2011-08-01'
union all
select '2011-08-02'
union all
select '2011-08-02'
union all
select '2011-08-03'
union all
select '2011-08-04'
union all
select '2011-08-05'
union all
select '2011-08-06'
union all
select '2011-08-07'
union all
select '2011-08-08'
union all
select '2011-08-08'
union all
select '2011-08-09'
union all
select '2011-08-10'
union all
select '2011-08-12'
union all
select '2011-08-13'
union all
select '2011-08-14'
union all
select '2011-08-15'
union all
select '2011-08-16'
union all
select '2011-08-17'
union all
select '2011-08-18'
union all
select '2011-08-19'
union all
select '2011-08-20'
union all
select '2011-08-21'
go
declare @md datetime
select @md = min(dt) from #t
;with ach as
(
select convert(varchar(10),dt,120) dt,
(datediff(dd,@md,convert(varchar(10),dt,120))-row_number() over (order by convert(varchar(10),dt,120))) as rid
from #t
group by convert(varchar(10),dt,120)
)
select a.id,a.dt,b.px
from #t a left join
(
select *,(row_number() over (partition by rid order by dt)-1)%8 + 1 as px
from ach
) b on convert(varchar(10),a.dt,120) = b.dt
drop table #t
/***********************
id dt px
----------- ----------------------- --------------------
1 2011-08-01 00:00:00.000 1
2 2011-08-02 00:00:00.000 2
3 2011-08-02 00:00:00.000 2
4 2011-08-03 00:00:00.000 3
5 2011-08-04 00:00:00.000 4
6 2011-08-05 00:00:00.000 5
7 2011-08-06 00:00:00.000 6
8 2011-08-07 00:00:00.000 7
9 2011-08-08 00:00:00.000 8
10 2011-08-08 00:00:00.000 8
11 2011-08-09 00:00:00.000 1
12 2011-08-10 00:00:00.000 2
13 2011-08-12 00:00:00.000 1
14 2011-08-13 00:00:00.000 2
15 2011-08-14 00:00:00.000 3
16 2011-08-15 00:00:00.000 4
17 2011-08-16 00:00:00.000 5
18 2011-08-17 00:00:00.000 6
19 2011-08-18 00:00:00.000 7
20 2011-08-19 00:00:00.000 8
21 2011-08-20 00:00:00.000 1
22 2011-08-21 00:00:00.000 2
(22 行受影响)
select *, row_number() over(PARTITION BY (id-1)/8 order by dt asc) from #t
create table #t(id int identity(1,1),dt datetime )
insert into #t(dt)
select '2011-08-01'
union all
select '2011-08-02'
union all
select '2011-08-03'
union all
select '2011-08-04'
union all
select '2011-08-05'
union all
select '2011-08-06'
union all
select '2011-08-07'
union all
select '2011-08-08'
union all
select '2011-08-09'
union all
select '2011-08-10'
union all
select '2011-08-11'
union all
select '2011-08-12'
union all
select '2011-08-13'
union all
select '2011-08-14'
union all
select '2011-08-15'
union all
select '2011-08-16'
union all
select '2011-08-17'
union all
select '2011-08-18'
union all
select '2011-08-19'
union all
select '2011-08-20'
union all
select '2011-08-21'
select *, row_number() over(PARTITION BY convert(int,(id-1)/8) order by dt asc) from #t
drop table #t
---------------------------------
1 2011-08-01 00:00:00.000 1
2 2011-08-02 00:00:00.000 2
3 2011-08-03 00:00:00.000 3
4 2011-08-04 00:00:00.000 4
5 2011-08-05 00:00:00.000 5
6 2011-08-06 00:00:00.000 6
7 2011-08-07 00:00:00.000 7
8 2011-08-08 00:00:00.000 8
9 2011-08-09 00:00:00.000 1
10 2011-08-10 00:00:00.000 2
11 2011-08-11 00:00:00.000 3
12 2011-08-12 00:00:00.000 4
13 2011-08-13 00:00:00.000 5
14 2011-08-14 00:00:00.000 6
15 2011-08-15 00:00:00.000 7
16 2011-08-16 00:00:00.000 8
17 2011-08-17 00:00:00.000 1
18 2011-08-18 00:00:00.000 2
19 2011-08-19 00:00:00.000 3
20 2011-08-20 00:00:00.000 4
21 2011-08-21 00:00:00.000 5
select *,no=null into #tb from #t
declare @num int=0,@dt datetime
update #tb set no=@num,
@num=case when @num<8 and datediff(day,@dt,dt)=1 then @num+1
when datediff(day,@dt,dt)=0 then @num else 1 end,
@dt=dt
select * from #tb
drop table #tb
/*
id dt no
----------- ----------------------- -----------
1 2011-08-01 00:00:00.000 1
2 2011-08-02 00:00:00.000 2
3 2011-08-02 00:00:00.000 2
4 2011-08-03 00:00:00.000 3
5 2011-08-04 00:00:00.000 4
6 2011-08-05 00:00:00.000 5
7 2011-08-06 00:00:00.000 6
8 2011-08-07 00:00:00.000 7
9 2011-08-08 00:00:00.000 8
10 2011-08-08 00:00:00.000 8
11 2011-08-09 00:00:00.000 1
12 2011-08-10 00:00:00.000 2
13 2011-08-12 00:00:00.000 1
14 2011-08-13 00:00:00.000 2
15 2011-08-14 00:00:00.000 3
16 2011-08-15 00:00:00.000 4
17 2011-08-16 00:00:00.000 5
18 2011-08-17 00:00:00.000 6
19 2011-08-18 00:00:00.000 7
20 2011-08-19 00:00:00.000 8
21 2011-08-20 00:00:00.000 1
22 2011-08-21 00:00:00.000 2
(22 行受影响)