0

I have a simple query that tries to select a distinct Country column from the Customer table, but it is returning duplicate values.

Here is the table looks like enter image description here

Country     City        PostalCode  CustomerName                    ContactName
USA         Lander      82520       Split Rail Beer & Ale           Art Braunschweiger
Germany     Leipzig     4179        Morgenstern Gesundkost          Alexander Feuer
France      Lille       59000       Folies gourmandes               Martine Rancé
Portugal    Lisboa      1675        Furia Bacalhau e                Lino Rodriguez
Portugal    Lisboa      1756        Princesa Isabel Vinhoss         Isabel de Castro
UK          London      WA11DP      Around the Horn                 Thomas Hardy
UK          London      EC25NT      B's Beverages                   Victoria Ashworth
UK          London      WX16LT      Consolidated Holdings           Elizabeth Brown
UK          London      WX36FW      Eastern Connection              Ann Devon

I want to get only distinct country name and it doesn't matter other column result. So I'm running below SQL code but it keep giving me some duplicate values.


with all_data as (SELECT distinct Country , City, PostalCode, CustomerName,ContactName FROM Customers),

distinct_country as (
SELECT distinct Country FROM Customers
),
distinct_values as (
SELECT t.Country , a.City, a.PostalCode, a.CustomerName,a.ContactName 
FROM distinct_country t
left join 
all_data a
on t.Country = a.Country
)
select * from distinct_values 

I'm looking like the below result. (the distinct country name and it doesn't matter other column result)

Country     City        PostalCode  CustomerName                    ContactName
USA         Lander      82520       Split Rail Beer & Ale           Art Braunschweiger
Germany     Leipzig     4179        Morgenstern Gesundkost          Alexander Feuer
France      Lille       59000       Folies gourmandes               Martine Rancé
Portugal    Lisboa      1675        Furia Bacalhau e                Lino Rodriguez
UK          London      WA11DP      Around the Horn                 Thomas Hardy

How can I do that? Thanks in advance!

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
rra
  • 809
  • 1
  • 8
  • 20

2 Answers2

1

If you only need the countries and the other fields doesn't care, just select that one and remove duplicates with GROUP BY

SELECT 
Country 
FROM all_data
GROUP BY Country

If you need to show the other fields, you have to put some criteria for the GROUP BY

Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
1

You haven't said what dbms you're on. This works on SQL Server.

ROW_NUMBER is the magic you're looking for. The CTE adds a row number that counts up on each row but starts over for each country. You then select only the rows where the row number is 1.

FIDDLE

;WITH CTE AS
  (SELECT Country
        , City
        , PostalCode
        , CustomerName
        , ContactName
        , ROW_NUMBER() OVER (PARTITION BY Country ORDER BY Country) rn
     FROM data 
  )
SELECT Country
     , City
     , PostalCode
     , CustomerName
     , ContactName
  FROM CTE
 WHERE rn = 1
Country City PostalCode CustomerName ContactName
France Lille 59000 Folies gourmandes Martine Rancé
Germany Leipzig 4179 Morgenstern Gesundkost Alexander Feuer
Portugal Lisboa 1675 Furia Bacalhau e Lino Rodriguez
UK London WA11DP Around the Horn Thomas Hardy
USA Lander 82520 Split Rail Beer & Ale Art Braunschweiger
Tom Boyd
  • 385
  • 1
  • 7