我有下表:
----------------------------------------------------------- ID oDate oName oItem oQty oRemarks ----------------------------------------------------------- 1 2016-01-01 A 001 2 2 2016-01-01 A 002 1 test 3 2016-01-01 B 001 3 4 2016-01-02 B 001 2 5 2016-01-02 C 001 2 6 2016-01-03 B 002 1 7 2016-01-03 B 001 4 ff.
我想获取每个名字的最新记录。所以结果应该是这样的:
----------------------------------------------------------- oDate oName oItem oQty oRemarks ----------------------------------------------------------- 2016-01-01 A 001 2 2016-01-01 A 002 1 test 2016-01-02 C 001 2 2016-01-03 B 002 1 2016-01-03 B 001 4 ff.
有谁知道该怎么做才能得到这个结果? 谢谢你。
该rank窗口子句允许你,好了,根据一些分区排名行,然后你可以只选择顶部的:
rank
SELECT oDate, oName, oItem, oQty, oRemarks FROM (SELECT oDate, oName, oItem, oQty, oRemarks, RANK() OVER (PARTITION BY oName ORDER BY oDate DESC) AS rk FROM my_table) t WHERE rk = 1