日期:2014-05-18  浏览次数:20707 次

不要用游标!
原始数据:
ID date line p_start p_end flag
1 2010/1/1 AAA-BBB AAA BBB 5
2 2010/1/4 AAA-BBB AAA BBB 5
3 2010/1/9 AAA-BBB AAA BBB 5
4 2010/1/10 AAA-BBB AAA BBB 6
5 2010/1/11 AAA-BBB AAA BBB 5
6 2010/1/12 AAA-BBB AAA BBB 6
7 2010/1/14 AAA-BBB AAA BBB 6
8 2010/1/15 AAA-BBB AAA BBB 6
9 2010/1/16 AAA-BBB AAA BBB 6
10 2010/1/18 AAA-BBB AAA BBB 6
11 2010/1/19 AAA-BBB AAA BBB 5
12 2010/1/20 AAA-BBB AAA BBB 5
13 2010/1/22 AAA-BBB AAA BBB 6
14 2010/1/23 AAA-BBB AAA BBB 6
15 2010/1/25 AAA-BBB AAA BBB 3
16 2010/1/27 AAA-BBB AAA BBB 3
17 2010/1/31 AAA-BBB AAA BBB 3
18 2010/2/1 AAA-BBB AAA BBB 3
19 2010/2/2 AAA-BBB AAA BBB 3
20 2010/2/3 AAA-BBB AAA BBB 3
21 2010/2/5 AAA-BBB AAA BBB 3
22 2010/2/6 AAA-BBB AAA BBB 3
23 2010/2/8 AAA-BBB AAA BBB 3
24 2010/2/9 AAA-BBB AAA BBB 3
25 2010/2/10 AAA-BBB AAA BBB 3
26 2010/2/11 AAA-BBB AAA BBB 4
27 2010/2/12 AAA-BBB AAA BBB 4
28 2010/2/13 AAA-BBB AAA BBB 4
29 2010/2/14 AAA-BBB AAA BBB 4
30 2010/2/15 AAA-BBB AAA BBB 4
31 2011/4/9 CCC-DDD CCC DDD 6
32 2011/4/11 CCC-DDD CCC DDD 6
33 2011/4/13 CCC-DDD CCC DDD 6
34 2011/4/16 CCC-DDD CCC DDD 0
35 2011/4/18 CCC-DDD CCC DDD 6
36 2011/4/20 CCC-DDD CCC DDD 6
37 2011/4/23 CCC-DDD CCC DDD 6
38 2011/4/25 CCC-DDD CCC DDD 6
39 2011/4/27 CCC-DDD CCC DDD 6
40 2011/4/30 CCC-DDD CCC DDD 6
41 2011/5/2 CCC-DDD CCC DDD 6
42 2011/5/4 CCC-DDD CCC DDD 6
43 2011/5/7 CCC-DDD CCC DDD 6
44 2011/5/9 CCC-DDD CCC DDD 5
45 2011/5/11 CCC-DDD CCC DDD 5
46 2011/5/14 CCC-DDD CCC DDD 5
47 2011/5/16 CCC-DDD CCC DDD 6
48 2011/5/18 CCC-DDD CCC DDD 5
49 2011/5/21 CCC-DDD CCC DDD 5
50 2011/5/23 CCC-DDD CCC DDD 5
51 2011/5/25 CCC-DDD CCC DDD 5
52 2011/5/28 CCC-DDD CCC DDD 5
53 2011/5/30 CCC-DDD CCC DDD 6
54 2011/6/1 CCC-DDD CCC DDD 6
55 2011/6/4 CCC-DDD CCC DDD 6
56 2011/6/6 CCC-DDD CCC DDD 6
57 2011/6/8 CCC-DDD CCC DDD 6
58 2011/6/11 CCC-DDD CCC DDD 5
要求:对于相同的line、相同的p_start、相同的p_end,对于flag<>6的记录将日期合并成一个区间,要求得到如下结果表:
COL1 COL2 COL3 COL4 COL5 COL6
AAA-BBB AAA BBB 2010/1/1 2010/1/9 5
AAA-BBB AAA BBB 2010/1/11 2010/1/11 5
AAA-BBB AAA BBB 2010/1/19 2010/1/20 5
AAA-BBB AAA BBB 2010/1/25 2010/2/10 3
AAA-BBB AAA BBB 2010/2/11 2010/2/15 4
CCC-DDD CCC DDD 2011/4/16 2011/4/16 0
CCC-DDD CCC DDD 2011/5/9 2011/5/14 5
CCC-DDD CCC DDD 2011/5/18 2011/5/28 5
CCC-DDD CCC DDD 2011/6/11 2011/6/11 5


------解决方案--------------------
select line,p_start,p_end,min(date) , max(date) , count(1) from tb where flag<>6 group by line,p_start,p_end
------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([ID] int,[date] datetime,[line] varchar(7),[p_start] varchar(3),[p_end] varchar(3),[flag] int)
insert [tb]
select 1,'2010/1/1','AAA-BBB','AAA','BBB',5 union all
select 2,'2010/1/4','AAA-BBB','AAA','BBB',5 union all
select 3,'2010/1/9','AAA-BBB','AAA','BBB',5 union all
select 4,'2010/1/10','AAA-BBB','AAA','BBB',6 union all
select 5,'2010/1/11','AAA-BBB','AAA','BBB',5 union all
select 6,'2010/1/12','AAA-BBB','AAA','BBB',6 union all
select 7,'2010/1/14','AAA-BBB','AAA','BBB',6 union all
select 8,'2010/1/15','AAA-BBB','AAA','BBB',6 union all
select 9,'2010/1/16','AAA-BBB','AAA','BBB',6 union all
select 10,'2010/1/18','AAA-BBB','AAA','BBB',6 union all
select 11,'2010/1/19','AAA-BBB','AAA','BBB',5 union all
select 12,'2010/1/20','AAA-BBB','AAA','BBB',5 union all
select 13,'2010/1/22','AAA-BBB','AAA','BBB',6 union all
select 14,'2010/1/23','AAA-BBB','AAA','BBB',6 union all
select 15,'2010/1/25','AAA-BBB','AAA','BBB',3 union all
select 16,'2010/1/27','AAA-BBB','AAA','BBB',3 union all
select 17,'2010/1/31','AAA-B