2

I'm using postgresql and I have two columns Country and Name. I want to get x numbers of people's name for each country.

so forexample if I had a data like this

Name    Country
"John"  "US"
"Kim"   "KR"
"Mike"  "US"
"Park"  "KR"
"Kim"   "US"
"Doe"   "RU"
"Pou"   "KR"
"John"  "RU"
"Sam"   "RU"
 ...    ...
 ...    ...

and say I want to get 2 ppl name for each country

Name    Country
"John"  "US"
"Mike"  "US"



"Park"  "KR"
"Pou"   "KR"



"Sam"   "RU"
"Doe"   "RU"

is there a way to do this kind of stuff?

James Park
  • 165
  • 1
  • 2
  • 14
  • You might want to be a little more specific but the general query you would need is `SELECT Name, Country FROM [table] GROUP BY Name, Country ORDER BY Country` – Lucas Jan 21 '19 at 09:26

1 Answers1

4

You can try below - using row_number()

select * from
(
SELECT Name, Country, row_number() over(partition by country order by name) as rn
FROM [table] 
)A where rn<=10 [here x=10]
Fahmi
  • 37,315
  • 5
  • 22
  • 31