Question

I have this query

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

that returns this result set (3392 records)

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

From this result set I would like to return the most used vendor for the product

ProdID      AROVend RN
026819143   26815   1
026900102   83551   2
061812104   80705   1
061820327   100538  2
061820578   69124   2
061824934   93617   1
061825815   30392   1

I have tried to wrap the above query in another query to use Row_Number to rank my Product per Vendor:

Select ProductID, ARPVend, 
 Row_Number() over(partition by ProductID Order by rn desc)
from (query above)a

This query also returns 3392 records when I would expect there to be less records. Any suggestions as to what I am doing incorrectly?

Thanks for any help.

leslie

Was it helpful?

Solution

The question is unclear, but if you want to get the second set from first you have metioned in post, use this:

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
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top