select Bh=(select count(distinct Name) from TB
           where Name>=a.Name),Name,Item
from TB as a

解决方案 »

  1.   

    select (select count(distinct Name) from TB where Name<=a.Name ) as Bh,
    Name,
    Item
    from TB a
    order by Name
      

  2.   

    create table TB (Name varchar(10),Item Varchar(20))insert into TB(Name,Item)
    select '電腦','內存'
    union select '電腦','CPU'
    union select '電腦','顯示'
    union select '衣服','牛'
    union select '衣服','西裝'
    union select '電器','影響'
    union select '電器','录影机'select IDENTITY(int ,1,1) as aa ,name   into  #TT from TB  group by name
    select b.AA ,A.*  FROM TB a LEFT JOIN #TT B ON (A.NAME=B.NAME)
    drop table #TT,TB