求一条显示每组最大序号记录的语句
T1
组编码 序号 名称 规格 单位 价格 。。。。
1001 1 名称1 1 包 50 。。。。
1001 2 名称1 1 包 55 。。。。
1002 1 名称2 1 包 50 。。。。
1003 1 名称3 1 包 60 。。。。
1003 2 名称3 1 包 50 。。。。
1003 3 名称3 1 包 40 。。。。
1004 1 名称4 1 包 50 。。。。
结果
组编码 序号 名称 规格 单位 价格 。。。。
1001 2 名称1 1 包 55 。。。。
1002 1 名称2 1 包 50 。。。。
1003 3 名称3 1 包 40 。。。。
1004 1 名称4 1 包 50 。。。。
[解决办法]
select * from tb a
where not exists(select 1 from tb where 组编码=a.组编码 and 序号>a.序号)
[解决办法]
with tb as( select 组编码, max(序号) as 序号 from T1 group by 组编码)select T1.*from T1inner 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 awhere a.Row=1
[解决办法]
1、select * from tb a where not exists(select 1 from tb where 组编码=a.组编码 and 序号>a.序号)2、select * from tb awhere (select count(1) from tb where 组编码=a.组编码 and 序号>a.序号)=03、select * from tb awhere 序号=(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 awhere a.rn=1
[解决办法]
select 组编码,max(序号),名称,规格,单位,价格 from T1group by 组编码,名称,规格,单位,价格