0

Assume that I have one table looking like this:

ID ClientID Name Country
1 JX100 John Canada
2 JX100 John Japan
3 JX690 Rob EUA
4 PX301 Alice France

And My query:

SELECT DISTINCT ClientID,Name,Country FROM CLIENTS

OUTPUT:

ClientID Name Country
JX100 John Canada
JX100 John Japan
JX690 Rob EUA
PX301 Alice France

I want to take that:

DESIRED:

ClientID Name Country
JX100 John Canada
JX690 Rob EUA
PX301 Alice France

TL;DR

I just want to select one country for all Clients, I don't want to get repeated rows.

Thom A
  • 88,727
  • 11
  • 45
  • 75
Renan Duarte
  • 138
  • 1
  • 10

1 Answers1

0

You can use row_number()over() with common table expression like below:

with cte as (
SELECT DISTINCT ClientID,Name,Country,row_number()over(partition by  ClientID   ,Name    order by Country) rownumber FROM CLIENTS)
select * from cte where rownumber=1

Row_number()over(partition by ClientID ,Name order by Country) will generate a sequence for contries starting from 1 against each clientid and name. So when you will select rownumber=1 this query will select one country for a single clientid,name combination.