日期:2014-05-18 浏览次数:20511 次
select * from tb t where billdate=(select max(billdate) from tb where goodsid=t.goodsid)
------解决方案--------------------
if object_id('[TB]') is not null drop table [TB] go create table [TB] (billdate datetime,goodsid int,userdef1 nvarchar(2),userdef2 nvarchar(2),userdef3 nvarchar(2),userdef4 nvarchar(2)) insert into [TB] select '2011-11-01',1001,'a','b','c','d' union all select '2011-11-01',1001,'b','c','d','e' union all select '2011-11-01',3002,'c','d','e','f' union all select '2011-11-08',3002,'e','f','g','h' union all select '2011-11-11',3013,'e','f','g','h' select * from [TB] SELECT * FROM TB WHERE EXISTS(SELECT 1 FROM TB A WHERE TB.goodsid = goodsid AND TB.USERDEF1 >USERDEF1 ) /* billdate goodsid userdef1 userdef2 userdef3 userdef4 2011-11-01 00:00:00.000 1001 b c d e 2011-11-08 00:00:00.000 3002 e f g h*/
------解决方案--------------------
--小F这个好,有单一值的时候就好用 select * from tb t where userdef1=(select max(userdef1) from tb where goodsid=t.goodsid) ORDER BY t.billdate ASC /* billdate goodsid userdef1 userdef2 userdef3 userdef4 2011-11-01 00:00:00.000 1001 b c d e 2011-11-08 00:00:00.000 3002 e f g h 2011-11-11 00:00:00.000 3013 e f g h*/