【发布时间】:2019-07-11 20:02:22
【问题描述】:
declare @t table (
Id int ,
Section int,
Moment date
);
insert into @t values
( 1 , 1 , '2014-01-01'),
( 2 , 1 , '2014-01-02'),
( 3 , 1 , '2014-01-03'),
( 4 , 1 , '2014-01-04'),
( 5 , 1 , '2014-01-05'),
( 6 , 2 , '2014-02-06'),
( 7 , 2 , '2014-02-07'),
( 8 , 2 , '2014-02-08'),
( 9 , 2 , '2014-02-09'),
( 10 , 2 , '2014-02-10'),
( 11 , 3 , '2014-03-11'),
( 12 , 3 , '2014-03-12'),
( 13 , 3 , '2014-03-13'),
( 14 , 3 , '2014-03-14'),
( 15 , 3 , '2014-03-15');
获取这样的数据
select * from @t
Id Section Moment
1 1 2014-01-01
2 1 2014-01-02
3 1 2014-01-03
4 1 2014-01-04
5 1 2014-01-05
6 2 2014-02-06
7 2 2014-02-07
8 2 2014-02-08
9 2 2014-02-09
10 2 2014-02-10
11 3 2014-03-11
12 3 2014-03-12
13 3 2014-03-13
14 3 2014-03-14
15 3 2014-03-15
但我想要 this.group by 3 和 Section wise 之类的数据
如果 ant Section 有 5 行,则会创建 2 个组。
Id Section Moment Group by 3
1 1 1/1/2014 1
2 1 1/2/2014 1
3 1 1/3/2014 1
4 1 1/4/2014 2
5 1 1/5/2014 2
6 2 2/6/2014 3
7 2 2/7/2014 3
8 2 2/8/2014 3
9 2 2/9/2014 4
10 2 2/10/2014 4
11 3 3/11/2014 5
12 3 3/12/2014 5
13 3 3/13/2014 5
14 3 3/14/2014 6
15 3 3/15/2014 6
【问题讨论】:
标签: sql database sql-server-2008