当前位置: 代码迷 >> Sql Server >> 不要用游标!解决办法
  详细解决方案

不要用游标!解决办法

热度:18   发布时间:2016-04-27 13:54:50.0
不要用游标!
原始数据:
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]gocreate 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 allselect 2,'2010/1/4','AAA-BBB','AAA','BBB',5 union allselect 3,'2010/1/9','AAA-BBB','AAA','BBB',5 union allselect 4,'2010/1/10','AAA-BBB','AAA','BBB',6 union allselect 5,'2010/1/11','AAA-BBB','AAA','BBB',5 union allselect 6,'2010/1/12','AAA-BBB','AAA','BBB',6 union allselect 7,'2010/1/14','AAA-BBB','AAA','BBB',6 union allselect 8,'2010/1/15','AAA-BBB','AAA','BBB',6 union allselect 9,'2010/1/16','AAA-BBB','AAA','BBB',6 union allselect 10,'2010/1/18','AAA-BBB','AAA','BBB',6 union allselect 11,'2010/1/19','AAA-BBB','AAA','BBB',5 union allselect 12,'2010/1/20','AAA-BBB','AAA','BBB',5 union allselect 13,'2010/1/22','AAA-BBB','AAA','BBB',6 union allselect 14,'2010/1/23','AAA-BBB','AAA','BBB',6 union allselect 15,'2010/1/25','AAA-BBB','AAA','BBB',3 union allselect 16,'2010/1/27','AAA-BBB','AAA','BBB',3 union allselect 17,'2010/1/31','AAA-BBB','AAA','BBB',3 union allselect 18,'2010/2/1','AAA-BBB','AAA','BBB',3 union allselect 19,'2010/2/2','AAA-BBB','AAA','BBB',3 union allselect 20,'2010/2/3','AAA-BBB','AAA','BBB',3 union allselect 21,'2010/2/5','AAA-BBB','AAA','BBB',3 union allselect 22,'2010/2/6','AAA-BBB','AAA','BBB',3 union allselect 23,'2010/2/8','AAA-BBB','AAA','BBB',3 union allselect 24,'2010/2/9','AAA-BBB','AAA','BBB',3 union allselect 25,'2010/2/10','AAA-BBB','AAA','BBB',3 union allselect 26,'2010/2/11','AAA-BBB','AAA','BBB',4 union allselect 27,'2010/2/12','AAA-BBB','AAA','BBB',4 union allselect 28,'2010/2/13','AAA-BBB','AAA','BBB',4 union allselect 29,'2010/2/14','AAA-BBB','AAA','BBB',4 union allselect 30,'2010/2/15','AAA-BBB','AAA','BBB',4 union allselect 31,'2011/4/9','CCC-DDD','CCC','DDD',6 union allselect 32,'2011/4/11','CCC-DDD','CCC','DDD',6 union allselect 33,'2011/4/13','CCC-DDD','CCC','DDD',6 union allselect 34,'2011/4/16','CCC-DDD','CCC','DDD',0 union allselect 35,'2011/4/18','CCC-DDD','CCC','DDD',6 union allselect 36,'2011/4/20','CCC-DDD','CCC','DDD',6 union allselect 37,'2011/4/23','CCC-DDD','CCC','DDD',6 union allselect 38,'2011/4/25','CCC-DDD','CCC','DDD',6 union allselect 39,'2011/4/27','CCC-DDD','CCC','DDD',6 union allselect 40,'2011/4/30','CCC-DDD','CCC','DDD',6 union allselect 41,'2011/5/2','CCC-DDD','CCC','DDD',6 union allselect 42,'2011/5/4','CCC-DDD','CCC','DDD',6 union allselect 43,'2011/5/7','CCC-DDD','CCC','DDD',6 union allselect 44,'2011/5/9','CCC-DDD','CCC','DDD',5 union allselect 45,'2011/5/11','CCC-DDD','CCC','DDD',5 union allselect 46,'2011/5/14','CCC-DDD','CCC','DDD',5 union allselect 47,'2011/5/16','CCC-DDD','CCC','DDD',6 union allselect 48,'2011/5/18','CCC-DDD','CCC','DDD',5 union allselect 49,'2011/5/21','CCC-DDD','CCC','DDD',5 union allselect 50,'2011/5/23','CCC-DDD','CCC','DDD',5 union allselect 51,'2011/5/25','CCC-DDD','CCC','DDD',5 union allselect 52,'2011/5/28','CCC-DDD','CCC','DDD',5 union allselect 53,'2011/5/30','CCC-DDD','CCC','DDD',6 union allselect 54,'2011/6/1','CCC-DDD','CCC','DDD',6 union allselect 55,'2011/6/4','CCC-DDD','CCC','DDD',6 union allselect 56,'2011/6/6','CCC-DDD','CCC','DDD',6 union allselect 57,'2011/6/8','CCC-DDD','CCC','DDD',6 union allselect 58,'2011/6/11','CCC-DDD','CCC','DDD',5go-->数据查询:select line,p_start,p_end,min(date) as startdate,max(date) as enddate,flagfrom(select gid=id-(select count(1) from tb where line=a.line and p_start=a.p_start and p_end=a.p_end and flag=a.flag and id<a.id),* from tb  awhere flag<>6 ) bgroup by line,p_start,p_end,flag,gidorder by startdate/**line    p_start p_end startdate               enddate                 flag------- ------- ----- ----------------------- ----------------------- -----------AAA-BBB AAA     BBB   2010-01-01 00:00:00.000 2010-01-09 00:00:00.000 5AAA-BBB AAA     BBB   2010-01-11 00:00:00.000 2010-01-11 00:00:00.000 5AAA-BBB AAA     BBB   2010-01-19 00:00:00.000 2010-01-20 00:00:00.000 5AAA-BBB AAA     BBB   2010-01-25 00:00:00.000 2010-02-10 00:00:00.000 3AAA-BBB AAA     BBB   2010-02-11 00:00:00.000 2010-02-15 00:00:00.000 4CCC-DDD CCC     DDD   2011-04-16 00:00:00.000 2011-04-16 00:00:00.000 0CCC-DDD CCC     DDD   2011-05-09 00:00:00.000 2011-05-14 00:00:00.000 5CCC-DDD CCC     DDD   2011-05-18 00:00:00.000 2011-05-28 00:00:00.000 5CCC-DDD CCC     DDD   2011-06-11 00:00:00.000 2011-06-11 00:00:00.000 5(9 行受影响)**/
  相关解决方案