Distinct on a group by

Hello,
I have the below query:
SELECT distinct
       V.date_create as CREATEDDATE,        
       LTRIM(RTRIM(V.VENDOR_CODE)) as VENDORCODE,     
       V.VENDOR_NAME as VENDOR_NAME,
min(po_num),ph.date_create,ww_buyer
		       
  FROM d_vendors V 
inner join m_pohead ph  on  V.VENDOR_CODE = ph.vendor_code

WHERE
V.date_create >= '11-01-2014'
group by v.date_create,v.vendor_code,v.vendor_name,ph.date_create,ww_buyer
order by vendorcode

Open in new window

Please let me know how to modify this query so that for a vendor code it returns only the least po_num ,currently its returning multiple Po's because of the grouping on each row.Po num are unique values in the m_pohead table
Star79Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Walter RitzelSenior Software EngineerCommented:
SELECT x.*
FROM (
SELECT V.date_create as CREATEDDATE,        
              LTRIM(RTRIM(V.VENDOR_CODE)) as VENDORCODE,     
             V.VENDOR_NAME as VENDOR_NAME,
            po_num,
            ph.date_create,
           ww_buyer,
          row_number() over(partition by v.vendor_code order by po_num) as rn
  FROM d_vendors V 
inner join m_pohead ph  on  V.VENDOR_CODE = ph.vendor_code
WHERE
V.date_create >= '11-01-2014') x
where x.rn = 1
order by x.vendorcode

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.