我有這個疑問ROW_NUMBER爲最常用的返回所有行
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條記錄,當我期望有更少的記錄時。 關於我在做什麼不正確的任何建議?
感謝您的任何幫助。
張國榮
是否更高RN表示更使用?另外,你使用的是什麼DBMS? – PinnyM
是更高的RN是最常用的供應商。這是SQL服務器。 – Leslie