简体   繁体   中英

SQL Distinct on First Column OpenQuery

I'm using an OpenQuery in SQL Server 2014 to retrieve data from Progress.

This is my query:

SELECT *
FROM OPENQUERY(PRG, 'SELECT "cde","dsc" FROM tblCodes') 

And it retrieves data like this:

cde     dsc
===     =====
one     test
one     another
one     value
two     goes
two     here
two     also
three   example

However, I need to have results look like this:

cde     dsc
===     =====
one     test
two     goes
three   example

How do I do this in an OpenQuery ?

In your open query your query should be like below and I recommend you to have the cde column as a numeric ID:

 WITH CTE AS (select cde,dsc,
    row_number() over(
                        partition by cde
                        order by cde
                    ) as rn

from tblCodes 
)
select cde,dsc from CTE 
where rn =1

Check the execution here: sqlfiddle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM