如何取得某字段内容连续相同的次数?
如表A:
ID      Value
-----   -------
1       AB
2       BC
3       BC
4       AB
5       BC
6       BC
7       BC
8       AB
9       AB
10      DE我如何取得Value字段里,BC值连续出现的最大次数是3?AB值连续出现的最大次数是2?

解决方案 »

  1.   

    declare @t table(ID int,Value varchar(4))
    insert into @t select 1 ,'AB'
    insert into @t select 2 ,'BC'
    insert into @t select 3 ,'BC'
    insert into @t select 4 ,'AB'
    insert into @t select 5 ,'BC'
    insert into @t select 6 ,'BC'
    insert into @t select 7 ,'BC'
    insert into @t select 8 ,'AB'
    insert into @t select 9 ,'AB'
    insert into @t select 10,'DE'
     
    select
        b.Value,max(b.num) as num
    from 
        (select
             a.Value,isnull((select min(ID) from @t where ID>a.ID and Value!=a.Value)-a.ID,0) as num
         from 
             @t a) b
    group by
        b.Value/*
    Value num         
    ----- ----------- 
    AB    2
    BC    3
    DE    0
    */
      

  2.   

    b.Value,max(b.num) as num
    这句不理解,能解释下吗?哪来的表b?
    另外:下面的有无快捷方法:
    insert into @t select 1 ,'AB'
    insert into @t select 2 ,'BC'
    insert into @t select 3 ,'BC'
    insert into @t select 4 ,'AB'
    insert into @t select 5 ,'BC'
    insert into @t select 6 ,'BC'
    insert into @t select 7 ,'BC'
    insert into @t select 8 ,'AB'
    insert into @t select 9 ,'AB'
    insert into @t select 10,'DE'
      

  3.   

    b是select a.Value,isnull((select min(ID) from @t where ID>a.ID and Value!=a.Value)-a.ID,0) as num from @t a结果集派生表的别名。----
    另外:下面的有无快捷方法:
    insert into @t select 1 ,'AB'
    insert into @t select 2 ,'BC'
    insert into @t select 3 ,'BC'
    insert into @t select 4 ,'AB'
    insert into @t select 5 ,'BC'
    insert into @t select 6 ,'BC'
    insert into @t select 7 ,'BC'
    insert into @t select 8 ,'AB'
    insert into @t select 9 ,'AB'
    insert into @t select 10,'DE'--上面的语句,是插入测试数据的。
      

  4.   

    结了。
    不知同样的功效,在Access数据库是如何实现?
    http://community.csdn.net/Expert/topic/4772/4772017.xml?temp=.7373926
      

  5.   

    可能写法略有不同吧,好像access里面没有Isnull函数