日期:2014-05-18 浏览次数:20561 次
select * from AAA a where not exists (select 1 from AAA where PN=a.PN and QTY>a.QTY)
------解决方案--------------------
select * from aaa t where qty=(select max(qty) from tb where pn=t.pn)
------解决方案--------------------
select * from aaa a where not exists (select 1 from aaa where PN=a.PN and QTY>a.QTY)
------解决方案--------------------
create table AAA([USER] varchar(10),PN varchar(20),QTY int) insert into AAA select 'O-10','306-077A',757 insert into AAA select 'O-16','306-077A',127 insert into AAA select 'G-42','306-077A',20 insert into AAA select 'B-40','306-179',12 insert into AAA select 'F-58','306-179',120 insert into AAA select 'H-23','306-224',50 go select * from AAA a where not exists(select 1 from AAA where PN=a.PN and QTY>a.QTY) /* 如果同一客户采购不止一笔,则要先统计后再比较: ;with cte as( select [USER],PN,sum(QTY)as QTY from tb group by [USER],PN )select * from cte a where not exists(select 1 from cte where PN=a.PN and QTY>a.QTY) */ go drop table AAA /* USER PN QTY ---------- -------------------- ----------- O-10 306-077A 757 F-58 306-179 120 H-23 306-224 50 (3 行受影响) */