select id from t1 order by idid
200702
200702
200704
200704
200705查询结果如上
我想变成如下结果
id
标项1
标项1
标项2
标项2
标项3
......以此类推

解决方案 »

  1.   

    select 
    '标项'+cast((select count(distinct id) from t1 where id<=a.id) as varchar) as id 
    from t1 a 
    order by id
      

  2.   

    select *,con=identity(int,1,1) into # 
    from  表名Aupdate 表名A
    set id='标项'+rtrim(a.con)
    from # a where a.id=表名A.id
    查询:
    select * from 表名A
      

  3.   

    declare @t table(id varchar(20))
    insert @t select '200702'
    union all select '200702'
    union all select '200704'
    union all select '200704'
    union all select '200705'select '标项'+rtrim((select count(distinct id) from @t where id<=a.id))  from @t a--结果
    ---------------- 
    标项1
    标项1
    标项2
    标项2
    标项3(所影响的行数为 5 行)
      

  4.   

    改一下:
    alter table ta add con int null
    declare @i int
    set @i=0
    udpate ta
    set con=@i,@i=@i+1update ta
    set id='标项'+rtrim(con)查询:
    select * from 表名A
      

  5.   

    看错,以上是递增生成记录,改一下
    alter table ta add con int null 新增递增列
    declare @i int
    set @i=0
    udpate ta
    set con=@i,@i=@i+1update ta
    set id='标项'+(select rtrim(count(distinct id)) from ta where con!>a.con)
    from ta aalter table ta drop column con --删除新增递增列select * from ta
      

  6.   

    越看越糊涂了!!原SQL
    select bdp_name,bdp_pinpai,bdp_bz,bdp_unit,bdp_num,yqghsj,bdp_desc,bdp_fzid  from bid_item a inner join bid_detailed_plan b on a.bi_id=b.bdp_pid where a.bi_id = '"&bi_id&"' order by bdp_fzid查询结果大意
    字段1    2     3        4  ....    bdp_fzid
    123     123   123     123         200702
    123     123   123     123         200702
    123     123   123     123         200703
    123     123   123     123         200703
    123     123   123     123         200705
    123     123   123     123         200705
    123     123   123     123         200706想得到这样
    字段1    2     3        4  ....    bdp_fzid
    123     123   123     123         标项1
    123     123   123     123         标项1
    123     123   123     123         标项2
    123     123   123     123         标项2
    123     123   123     123         标项3
    123     123   123     123         标项3
    123     123   123     123        标项4
      

  7.   

    上面几位的思想如何套到我的SQL语句中??
      

  8.   

    select bdp_name,bdp_pinpai,bdp_bz,bdp_unit,bdp_num,yqghsj,bdp_desc,(select '标项'+rtrim((select count(distinct bdp_fzid) from bid_detailed_plan where bdp_fzid<=b.bdp_fzid))) bx  from bid_item a inner join bid_detailed_plan b on a.bi_id=b.bdp_pid where a.bi_id = '20070202131249' order by bdp_fzid 字段1    2     3        4  ....    bdp_fzid
    123     123   123     123         标项2
    123     123   123     123         标项2
    123     123   123     123         标项3
    123     123   123     123         标项3
    123     123   123     123         标项4
    123     123   123     123         标项4
    123     123   123     123        标项5
    为什么是2开始而不是一开始?
      

  9.   

    create table T(col1 int, col2 varchar(10))
    insert T select  123, '200702'
    union all select 123, '200702'
    union all select 123, '200703'
    union all select 123, '200703'
    union all select 123, '200705'
    union all select 123, '200705'
    union all select 123, '200706'declare @col1 int, @col2 varchar(10), @col2_old varchar(10), @id int
    declare @t table(col1 int, col2 varchar(10))declare cur cursor local
    for
    select * from T
    open curfetch next from cur into @col1, @col2
    select @col2_old=@col2, @id=1
    while @@fetch_status=0
    begin
    if @col2_old=@col2
    insert @t select @col1, '标项'+rtrim(@id)  
    else
    begin
    select @id=@id+1, @col2_old=@col2
    insert @t select @col1, '标项'+rtrim(@id)  
    end fetch next from cur into @col1, @col2
    endclose cur
    deallocate curselect * from @t--result
    col1        col2       
    ----------- ---------- 
    123         标项1
    123         标项1
    123         标项2
    123         标项2
    123         标项3
    123         标项3
    123         标项4(7 row(s) affected)