ID   step  flag
1001  1     否
1001  2     否
1002  1     否
1002  2     否
1002  3     否
1002  4     是要变为ID    flag 
1001   否
1002   是
对于每个编号,看最后一个step的flag来生成新的flag,另外 我希望找一本这方面的基础书看看(主要是转化数据显示格式的),谁能推荐一下,谢谢了

解决方案 »

  1.   

    2006留个纪念:
    select table1.*
    from table1,(select ID,max(step) step from table1 group by ID) as t
    where table1.ID=t.ID and table1.step=t.step
      

  2.   

    --创建测试数据
    declare @t table(ID int,step int,flag varchar(10))
    insert @T
    select 1001,  1,     '否' union all
    select 1001,  2,     '否' union all
    select 1002,  1,     '否' union all
    select 1002,  2,     '否' union all
    select 1002,  3,     '否' union all
    select 1002,  4,     '是'select ID,flag from @t as a where step = (select top 1 step from @t where ID = a.ID order by step DESC)
    --或
    select ID,flag from @t as a where not exists(select 1 from @t where ID = a.ID and step > a.step)/*结果
    ID    flag 
    1001   否
    1002   是
    */
      

  3.   

    create table #temp
    (
    ID int,
    step int,
    flag varchar(5)
    )insert into #temp values(1001,1,'否')
    insert into #temp values(1001,2,'否')
    insert into #temp values(1002,1,'否')
    insert into #temp values(1002,2,'否')
    insert into #temp values(1002,3,'否')
    insert into #temp values(1002,4,'是')select * from #tempselect a.ID,flag from #temp a,
    (
    select ID,max(step) max_step
    from #temp
    group by ID
    ) b
    where a.ID=b.ID
    and a.step=b.max_step