日期:2014-05-17  浏览次数:20626 次

求三连、四连的SQL查询语句
列:有一表myTable,数据为:
id   col
1    A
2    B
3    C
4    A
5    A
6    A
7    C
8    B
9    B
10   B
11   D
12   A
13   A
14   A
15   A
16   A
17   C
表中N连的数据为:
3A  3B   5A 
最后,求出ABCD最大的连续:5A 和 3B

求解,求帮助!

------解决方案--------------------

with tb(id,col)as(
select 1,'A' union all
select 2,'B' union all
select 3,'C' union all
select 4,'A' union all
select 5,'A' union all
select 6,'A' union all
select 7,'C' union all
select 8,'B' union all
select 9,'B' union all
select 10,'B' union all
select 11,'D' union all
select 12,'A' union all
select 13,'A' union all
select 14,'A' union all
select 15,'A' union all
select 16,'A' union all
select 17,'C')
,tc as(
select col,number=id-row_number() over(partition by col order by id) from tb
)
select col,count(number) from tc
group by col,number
having count(number)>1

找出最大的,就接着写一个就行