user142253
user142253

Reputation: 524

Retrieve distinct SQL query results for one column and not the other column

I have a table that has the following format for the data. All I want is just one code and phone number. It does not matter which phone number I get, all I need it one phone number.

The results I need would look like: Code phoneNumber 1000009 (123)752-0108 1000257 (456)718-1229 1000259 (000)000-0001 1000270 (333)528-6468 1000276 (444)384-5571

Any help with the SQL query would be appreciated. Thank you

Upvotes: 2

Views: 123

Answers (2)

Joe Stefanelli
Joe Stefanelli

Reputation: 135818

WITH cteRowNum AS (
    SLEECT Code, phoneNumber,
           ROW_NUMBER() OVER(PARTITION BY Code ORDER BY phoneNumber) AS RowNum
        FROM YourTable
)
SELECT Code, phoneNumber
    FROM cteRowNum
    WHERE RowNum = 1;

Upvotes: 1

Lieven Keersmaekers
Lieven Keersmaekers

Reputation: 58451

If any of the phoneNumbers for a given code is sufficient, you can use a GROUP BY with any of the aggregating functions to accomplish just that

This example uses the MAX aggregating function.

SELECT  Code
        , PhoneNumber = MAX(phoneNumber)
FROM    Table
GROUP BY
        Code

Upvotes: 2

Related Questions