一个关于合并行记录的问题,谢谢大家

ID step flag
1001 1 否
1001 2 否
1002 1 否
1002 2 否
1002 3 否
1002 4 是
要变为
ID flag
1001 否
1002 是
对于每个编号,看最后一个step的flag来生成新的flag,
另外 我希望找一本这方面的基础书看看(主要是转化数据显示格式的),谁能推荐一下,谢谢了
[255 byte] By [anison-影舞者] at [2008-1-9]
# 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
pbsql-风云 at 2007-10-18 > top of Msdn China Tech,MS-SQL Server,基础类...
# 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 是
*/
hellowork-一两清风 at 2007-10-18 > top of Msdn China Tech,MS-SQL Server,基础类...
# 3
哈哈
两位还在奋斗啊
新年快乐
# 4
大家同乐!
今晚是个承前启后继往开来的时刻,所以一定要坚持从2006走进2007的.
hellowork-一两清风 at 2007-10-18 > top of Msdn China Tech,MS-SQL Server,基础类...
# 5
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 #temp

select 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
# 6
真是高手啊
watson110 at 2007-10-18 > top of Msdn China Tech,MS-SQL Server,基础类...