求一条SQL语句,求最近采购记录,谢谢
直接说问题:
有一个采购记录表,具有商品编号,采购日期,采购价等字段
商品编号 采购日期 采购价
10001 2011-02-03 10
10002 2011-02-03 15
10001 2011-03-05 12
10003 2011-03-05 14
10002 2011-03-14 16
10003 2011-03-14 13
需要结果
编号 日期 采购价
10001 2011-03-05 12
10002 2011-03-14 16
10003 2011-03-14 13
最所有商号的最新采购价即最后一次采购记录的价格,不重复,谢谢
[解决办法]
use Tempdbgo--> --> if not object_id(N'Tempdb..#T') is null drop table #TGoCreate table #T([商品编号] int,[采购日期] Datetime,[采购价] int)Insert #Tselect 10001,'2011-02-03',10 union allselect 10002,'2011-02-03',15 union allselect 10001,'2011-03-05',12 union allselect 10003,'2011-03-05',14 union allselect 10002,'2011-03-14',16 union allselect 10003,'2011-03-14',13GoSelect * from #T AS a WHERE NOT EXISTS(SELECT 1 FROM #T WHERE [商品编号]=a.[商品编号] AND [采购日期]>a.[采购日期])/*商品编号 采购日期 采购价10001 2011-03-05 00:00:00.000 1210002 2011-03-14 00:00:00.000 1610003 2011-03-14 00:00:00.000 13*/
[解决办法]