我正在尝试获取项目的最新数据,数据为Item,rank vend_num,vend_item和成本显然显示最新日期的记录。数据跨越两个表,这是我遇到的问题,我已经到了下面的查询下面,得到连接并输出数据。
SELECT itemvend_all.item, itemvend_all.rank, itemvend_all.vend_num,
itemvend_all.vend_item, itemvendprice_mst.brk_cost##1, itemvendprice_mst.effect_date
FROM itemvend_all
INNER JOIN itemvendprice_mst
ON itemvend_all.item = itemvendprice_mst.item
AND itemvend_all.vend_num = itemvendprice_mst.vend_num
WHERE (itemvend_all.rank = '1')
item rank vend_num vend_item brk_cost##1 effect_date
0100G 1 KUVEUR NULL 1 04/09/2013
0100G 1 KUVEUR NULL 2 01/11/2015
0120SQ 1 KUVEUR NULL 4 04/09/2013
0120SQ 1 KUVEUR NULL 5 01/11/2015
0120SQ 1 KUVEUR NULL 6 02/11/2015
输出应该是,下面还有几百个其他记录。
item rank vend_num vend_item brk_cost##1 effect_date
0100G 1 KUVEUR NULL 2 01/11/2015
0120SQ 1 KUVEUR NULL 6 02/11/2015
任何帮助都会很棒,
答案 0 :(得分:0)
您可以使用Windows函数row_number
来获取所需的输出
SELECT t1.* from (
SELECT ia.item, ia.rank, ia.vend_num, ia.vend_item, im.brk_cost##1, im.effect_date,row_number() over(partition by ia.item, ia.rank, ia.vend_num order by im.effect_date desc) rn
FROM itemvend_all ia INNER JOIN
itemvendprice_mst im ON ia.item = im.item AND ia.vend_num = im.vend_num
WHERE (ia.rank = '1')) as t1
WHERE RN=1