我有这个查询
select ProductID, ARPVend, count(distinct whsecode) as RN
FROM
(select pa.product_structure_fk, pid.value as productID,
ei.value as ARPVend, ei2.value as whsecode
from epacube.PRODUCT_ASSOCIATION pa
inner join epacube.PRODUCT_IDENTIFICATION pid
on pa.PRODUCT_STRUCTURE_FK = pid.PRODUCT_STRUCTURE_FK
inner join import.IMPORT_RECORD_DATA ird
on pid.VALUE = ird.DATA_POSITION20 and ird.JOB_FK = 1514
inner join epacube.ENTITY_IDENTIFICATION ei
on ei.ENTITY_STRUCTURE_FK = pa.ENTITY_STRUCTURE_FK
inner join epacube.ENTITY_IDENTIFICATION ei2
on pa.ORG_ENTITY_STRUCTURE_FK = ei2.ENTITY_STRUCTURE_FK
where pa.DATA_NAME_FK = 253501
UNION
SELECT distinct pid.Product_structure_fk,
data_position20, data_position3,
data_position78
from Import.IMPORT_RECORD_DATA ird
inner join epacube.product_identification pid
on ird.DATA_POSITION20 = pid.VALUE where ird.JOB_FK = 1514
)a
group by ProductID, ARPVend
返回此结果集(3392条记录)
ProdID ARPVend RN
026819143 26815 1
026900102 65187 1
026900102 83551 2
061812104 80705 1
061820327 80705 1
061820327 100538 2
061820578 74868 1
061820578 69124 2
061824934 93617 1
061825815 30392 1
从这个结果集中,我想返回产品最常用的供应商
ProdID AROVend RN
026819143 26815 1
026900102 83551 2
061812104 80705 1
061820327 100538 2
061820578 69124 2
061824934 93617 1
061825815 30392 1
我试图将上述查询包装在另一个查询中,以使用Row_Number对每个供应商的产品进行排名:
Select ProductID, ARPVend,
Row_Number() over(partition by ProductID Order by rn desc)
from (query above)a
当我希望记录更少时,此查询还会返回3392条记录。 关于我做错了什么的任何建议?
感谢您的帮助。
莱斯利
答案 0 :(得分:1)
问题尚不清楚,但如果你想从第一组获得第二套你在帖子中提到,请使用:
SELECT ProductID, ARPVend, RN
FROM
(
SELECT ProductID, ARPVend, RN,
Row_Number() OVER(PARTITION BY ProductID ORDER BY RN DESC) ROWNUM
FROM firstresultset
) A
WHERE ROWNUM = 1