本帖最后由 xiaopingatrry 于 2012-12-14 16:11:56 编辑

解决方案 »

  1.   


    declare @str nvarchar(100)
    set @str='KD01.0003.0002'
    select count(id) from #temp where charindex(@str,codes)>0
      

  2.   


    ---如果有ID这个字段,用上边的,没有用下边这个declare @str nvarchar(100)
    set @str='KD01.0003.0002'
    select count(codes) from #temp where charindex(@str,codes)>0
      

  3.   

    我要的是全部的一张表出来 格式为
    codes                         count
    KD01                           220
    KD01.0003                      100
    KD01.0003.0001                  50
    KD01.0003.0001.0001             50
    KD01.0003.0001.0001.0001        10
    KD01.0003.0001.0001.0002         5
    KD01.0003.0001.0001.0003         5
    这样的格式,谢谢,还有更好的方案吗?
      

  4.   

    不要变量哦 我的变量就是上面的codes 但是我不太会写什么循环  和存储
      

  5.   


    declare @str nvarchar(100)
    set @str='KD01.0003.0002'
    select @str as codes,count(codes) as count from #temp where charindex(@str,codes)>0
      

  6.   


    我不需要这个declare @str nvarchar(100) set @str='KD01.0003.0002'变量 我的变量是灵活的codes
      

  7.   

    --> 测试数据:[table1]
    if object_id('[table1]') is not null drop table [table1]
    GO
    create table [table1]([codes] varchar(24))
    insert [table1]
    select 'KD01' union all
    select 'KD01.0003' union all
    select 'KD01.0003.0001' union all
    select 'KD01.0003.0001.0001' union all
    select 'KD01.0003.0001.0001.0001' union all
    select 'KD01.0003.0001.0001.0002' union all
    select 'KD01.0003.0001.0001.0003' union all
    select 'KD01.0003.0001.0002' union all
    select 'KD01.0003.0001.0003' union all
    select 'KD01.0003.0001.0003.0001' union all
    select 'KD01.0003.0001.0003.0003' union all
    select 'KD01.0003.0001.0003.0004' union all
    select 'KD01.0003.0001.0003.0005' union all
    select 'KD01.0003.0001.0003.0007' union all
    select 'KD01.0003.0001.0003.0009' union all
    select 'KD01.0003.0001.0003.0010' union all
    select 'KD01.0003.0001.0003.0012' union all
    select 'KD01.0003.0001.0004' union all
    select 'KD01.0003.0001.0005' union all
    select 'KD01.0003.0001.0006' union all
    select 'KD01.0003.0002' union all
    select 'KD01.0003.0002.0001' union all
    select 'KD01.0003.0002.0002' union all
    select 'KD01.0003.0002.0002.0001' union all
    select 'KD01.0003.0002.0002.0003' union all
    select 'KD01.0003.0002.0002.0004' union all
    select 'KD01.0003.0002.0002.0005' union all
    select 'KD01.0003.0002.0003' union all
    select 'KD01.0003.0002.0003.0001' union all
    select 'KD01.0003.0002.0003.0002'
    select * from [table1] t
    CROSS APPLY 
         (SELECT [count]=COUNT(1) FROM table1 WHERE CHARINDEX(t.codes,codes)>0)a/*
    codes                    count
    ------------------------ -----------
    KD01                     30
    KD01.0003                29
    KD01.0003.0001           18
    KD01.0003.0001.0001      4
    KD01.0003.0001.0001.0001 1
    KD01.0003.0001.0001.0002 1
    KD01.0003.0001.0001.0003 1
    KD01.0003.0001.0002      1
    KD01.0003.0001.0003      9
    KD01.0003.0001.0003.0001 1
    KD01.0003.0001.0003.0003 1
    KD01.0003.0001.0003.0004 1
    KD01.0003.0001.0003.0005 1
    KD01.0003.0001.0003.0007 1
    KD01.0003.0001.0003.0009 1
    KD01.0003.0001.0003.0010 1
    KD01.0003.0001.0003.0012 1
    KD01.0003.0001.0004      1
    KD01.0003.0001.0005      1
    KD01.0003.0001.0006      1
    KD01.0003.0002           10
    KD01.0003.0002.0001      1
    KD01.0003.0002.0002      5
    KD01.0003.0002.0002.0001 1
    KD01.0003.0002.0002.0003 1
    KD01.0003.0002.0002.0004 1
    KD01.0003.0002.0002.0005 1
    KD01.0003.0002.0003      3
    KD01.0003.0002.0003.0001 1
    KD01.0003.0002.0003.0002 1(30 行受影响)*/
    drop table [table1]
      

  8.   

    select
      a.codes,b.num
    from
      tb a 
    inner join
      (select codes,count(1) as num from tb WHERE CHARINDEX(t.codes,codes)>0)b
    on
       a.codes=b.codes