create table test(cxid int, cxdh5 varchar(30))
insert test
select 9,'A红蓝'
union all
select 10,'A红黑'
union all
select 12,'B青蓝'
union all
select 13,'C绿红'
union all
select 14,'D青红'
union all
select 20,'A黑黑'
union all
select 21,'C红红'
union all
select 22,'B蓝红'
select left(cxdh5,1),'最高有'+rtrim(count(1))+'次' from test group by left(cxdh5,1)不知道是不是这样

解决方案 »

  1.   

    declare @test table(cxid int, cxdh5 varchar(30))
    insert @test
    select 9,'A红蓝'
    union all
    select 10,'A红黑'
    union all
    select 12,'B青蓝'
    union all
    select 13,'C绿红'
    union all
    select 14,'D青红'
    union all
    select 20,'A黑黑'
    union all
    select 21,'C红红'
    union all
    select 22,'B蓝红'select cxid=left(cxdh5,1),记录='最高有'+rtrim(count(*))+'次' 
    from @test group by left(cxdh5,1)(所影响的行数为 8 行)cxid 记录                   
    ---- -------------------- 
    A    最高有3次
    B    最高有2次
    C    最高有2次
    D    最高有1次(所影响的行数为 4 行)
      

  2.   

    不好意思,我的意思是想求最高不出现的次数;
    如A 统计次数是从cxID=12 到cxID=14 ,有3次不出现A
      A 统计次数是从cxID=21 到cxID=22 ,有2次不出现A
     我要取最高的次数
    其它一样
      

  3.   

    小弟是新手!表达不是那好,请谅解
    如B 统计次数是从cxID=9 到cxID=10 ,有2次不出现B
      B 统计次数是从cxID=13 到cxID=21 ,有4次不出现B
     我要取最高的次数4如C统计次数是从cxID=9 到cxID=12 ,有3次不出现C
      C 统计次数是从cxID=14 到cxID=20 ,有2次不出现C
      C 统计次数是从cxID=22 到cxID=22 ,有1次不出现C
     我要取最高的次数3
    如D统计次数是从cxID=9 到cxID=13 ,有4次不出现D
      D 统计次数是从cxID=20 到cxID=22 ,有3次不出现D
     我要取最高的次数4
      

  4.   

    求出最高不连续出现次数:declare @test table(cxid int, cxdh5 varchar(30))
    insert @test
    select 9,'A红蓝'
    union all
    select 10,'A红黑'
    union all
    select 12,'B青蓝'
    union all
    select 13,'C绿红'
    union all
    select 14,'D青红'
    union all
    select 20,'A黑黑'
    union all
    select 21,'C红红'
    union all
    select 22,'B蓝红'declare @cxid int, @cxdh5 varchar(30), @maxA int, @maxB int, @maxC int, @maxD int
    declare @tmaxA int, @tmaxB int, @tmaxC int, @tmaxD int
    set @maxA=0
    set @maxB=0
    set @maxC=0
    set @maxD=0
    set @tmaxA=0
    set @tmaxB=0
    set @tmaxC=0
    set @tmaxD=0declare cursor_test cursor for
    select cxdh5
    from @test
    order by cxidopen cursor_test
    fetch next from cursor_test into @cxdh5
    while @@fetch_status=0
    begin
    if left(@cxdh5,1)='A'
    begin
    if @tmaxA>@maxA
    set @maxA=@tmaxA
    set @tmaxA=0
    end
    else
    begin
    set @tmaxA=@tmaxA+1
    end if left(@cxdh5,1)='B'
    begin
    if @tmaxB>@maxB
    set @maxB=@tmaxB
    set @tmaxB=0
    end
    else
    begin
    set @tmaxB=@tmaxB+1
    end if left(@cxdh5,1)='C'
    begin
    if @tmaxC>@maxC
    set @maxC=@tmaxC
    set @tmaxC=0
    end
    else
    begin
    set @tmaxC=@tmaxC+1
    end if left(@cxdh5,1)='D'
    begin
    if @tmaxD>@maxD
    set @maxD=@tmaxD
    set @tmaxD=0
    end
    else
    begin
    set @tmaxD=@tmaxD+1
    end
    fetch next from cursor_test into @cxdh5
    end
    close cursor_test
    deallocate cursor_testselect 'A  最高有'+cast(@maxA as varchar(5))+'次'
    ,'B  最高有'+cast(@maxB as varchar(5))+'次'
    ,'C  最高有'+cast(@maxC as varchar(5))+'次'
    ,'D  最高有'+cast(@maxD as varchar(5))+'次'
    (所影响的行数为 8 行)                                                                    
    ---------------- ---------------- ---------------- ---------------- 
    A  最高有3次         B  最高有4次         C  最高有3次         D  最高有4次(所影响的行数为 1 行)
      

  5.   

    谢谢您!eyumumc(雨幕)
    能不能将它简化成一条SQL语句呀!
      

  6.   


    create table test(cxid int, cxdh5 varchar(30))
    insert test
    select 9,'A红蓝'
    union all
    select 10,'A红黑'
    union all
    select 12,'B青蓝'
    union all
    select 13,'C绿红'
    union all
    select 14,'D青红'
    union all
    select 20,'A黑黑'
    union all
    select 21,'C红红'
    union all
    select 22,'B蓝红'
    GOselect cxdh5,'最高有'+rtrim(MAX(CNT))+'次' FROM 
    (
        select LEFT(B.cxdh5,1) cxdh5,A.cxid acxid,B.cxid bcxid,
        (select count(1) from test where cxid>ISNULL(A.cxid,0) and cxid<B.cxid) CNT
         from test A right join test B 
        on LEFT(B.cxdh5,1)=LEFT(A.cxdh5,1) 
        AND A.cxid=(select max(cxid) from test where LEFT(cxdh5,1)=LEFT(A.cxdh5,1) AND cxid<B.cxid )
    ) T GROUP BY cxdh5----- --------------------
    A     最高有3次
    B     最高有4次
    C     最高有3次
    D     最高有4次
      

  7.   

    xiequanqin(新的一天,新的起点!) 谢谢您! 稍等;
    我测试一下!马上给分
      

  8.   

    ODBC: 消息 0,级别 19,状态 1
    SqlDumpExceptionHandler: 进程 51 发生了严重的异常 c0000005 EXCEPTION_ACCESS_VIOLATION。SQL Server 将终止该进程。连接中断
    这个是什么问题呀!
      

  9.   

    SQL 系统级的错误。。SQL补丁打到多少了?
      

  10.   

    http://support.microsoft.com/kb/327424/zh-cn原因
    在 Sqlsort.dll 和 Sqlservr.exe 版本不兼容时会出现此行为。