日期:2014-05-17 浏览次数:20564 次
with tb as ( select 组编码, max(序号) as 序号 from T1 group by 组编码 ) select T1.* from T1 inner join tb on tb.组编码 = T1.组编码 and tb.序号 = T1.序号
------解决方案--------------------
select a.* from tb a left join tb b on a.组编码=b.组编码 and a.序号<b.序号 where b.组编码 is null
------解决方案--------------------
select * from (select *,row_number()over(partition by 组编码 order by 序号 desc) as Row from T1 )as a where a.Row=1
------解决方案--------------------
1、 select * from tb a where not exists(select 1 from tb where 组编码=a.组编码 and 序号>a.序号) 2、 select * from tb a where (select count(1) from tb where 组编码=a.组编码 and 序号>a.序号)=0 3、 select * from tb a where 序号=(select max(序号)from tb where 组编码=a.组编码)order by 序号 4、 select * from tb where 序号=(select top 1 序号 from tb where 组编码=a.组编码 order by 序号 desc) 5、 select * from tb a where 序号!<all(select 序号 from tb where 组编码=a.组编码) 6、-- sql 2005 以上版本 select * from (select *,rn=row_number()over(partition by 组编码 order by 序号 desc) from tb )as a where a.rn=1
------解决方案--------------------
select 组编码,max(序号),名称,规格,单位,价格 from T1 group by 组编码,名称,规格,单位,价格