Top Reputation user from all counties of world

1

Please login or register to vote for this query.

(click on this box to dismiss)

Craft CMS Meta

Q&A about the site for administrators, end users, developers and designers for Craft CMS

---------------------- query for DATA.STACKEXCHANGE.COM -----------------------
-- Top Reputation user from all counties of world
-------------------------------------------------------------------------------
-- Note 1: Built based on topic: https://meta.stackexchange.com/questions/348060/
-- Note 2: To generate blocks, used: https://github.com/pavletabata/stackexchange-query-for-all-countries/blob/master/helper-php-code-for-countries
-- Note 3: Code at github : https://github.com/pavletabata/stackexchange-query-for-all-countries

-------------------------------------------------------------------------------



select id, reputation, location
into #users
from users
where location is not null
and reputation > 1

;with 
countries as 
(
  select 1 id, 'Afghanistan' name union
  select 2, 'Albania' union
  select 3, 'Algeria' union
  select 4, 'American Samoa' union
  select 5, 'Andorra' union
  select 6, 'Angola' union
  select 7, 'Anguilla' union
  select 8, 'Antarctica' union
  select 9, 'Antigua And Barbuda' union
  select 10, 'Argentina' union
  select 11, 'Armenia' union
  select 12, 'Aruba' union
  select 13, 'Australia' union
  select 14, 'Austria' union
  select 15, 'Azerbaijan' union
  select 16, 'Bahamas' union
  select 17, 'Bahrain' union
  select 18, 'Bangladesh' union
  select 19, 'Barbados' union
  select 20, 'Belarus' union
  select 21, 'Belgium' union
  select 22, 'Belize' union
  select 23, 'Benin' union
  select 24, 'Bermuda' union
  select 25, 'Bhutan' union
  select 26, 'Bolivia' union
  select 27, 'Bosnia And Herzegovina' union
  select 28, 'Botswana' union
  select 29, 'Bouvet Island' union
  select 30, 'Brazil' union
  select 31, 'British Indian Ocean Territory' union
  select 32, 'Brunei Darussalam' union
  select 33, 'Bulgaria' union
  select 34, 'Burkina Faso' union
  select 35, 'Burundi' union
  select 36, 'Cambodia' union
  select 37, 'Cameroon' union
  select 38, 'Canada' union
  select 39, 'Cape Verde' union
  select 40, 'Cayman Islands' union
  select 41, 'Central African Republic' union
  select 42, 'Chad' union
  select 43, 'Chile' union
  select 44, 'China' union
  select 45, 'Christmas Island' union
  select 46, 'Cocos Islands' union
  select 47, 'Colombia' union
  select 48, 'Comoros' union
  select 49, 'Congo' union
  select 50, 'Cook Islands' union
  select 51, 'Costa Rica' union
  select 52, 'Cote D''ivoire' union
  select 53, 'Croatia' union
  select 54, 'Cuba' union
  select 55, 'Cyprus' union
  select 56, 'Czech Republic' union
  select 57, 'Denmark' union
  select 58, 'Djibouti' union
  select 59, 'Dominica' union
  select 60, 'Dominican Republic' union
  select 61, 'East Timor' union
  select 62, 'Ecuador' union
  select 63, 'Egypt' union
  select 64, 'El Salvador' union
  select 65, 'Equatorial Guinea' union
  select 66, 'Eritrea' union
  select 67, 'Estonia' union
  select 68, 'Ethiopia' union
  select 69, 'Falkland Islands' union
  select 70, 'Faroe Islands' union
  select 71, 'Fiji' union
  select 72, 'Finland' union
  select 73, 'France' union
  select 74, 'French Guiana' union
  select 75, 'French Polynesia' union
  select 76, 'French Southern Territories' union
  select 77, 'Gabon' union
  select 78, 'Gambia' union
  select 79, 'Georgia' union
  select 80, 'Germany' union
  select 81, 'Ghana' union
  select 82, 'Gibraltar' union
  select 83, 'Greece' union
  select 84, 'Greenland' union
  select 85, 'Grenada' union
  select 86, 'Guadeloupe' union
  select 87, 'Guam' union
  select 88, 'Guatemala' union
  select 89, 'Guinea' union
  select 90, 'Guinea-bissau' union
  select 91, 'Guyana' union
  select 92, 'Haiti' union
  select 93, 'Heard Island And Mcdonald Islands' union
  select 94, 'Honduras' union
  select 95, 'Hong Kong' union
  select 96, 'Hungary' union
  select 97, 'Iceland' union
  select 98, 'India' union
  select 99, 'Indonesia' union
  select 100, 'Iran' union
  select 101, 'Iraq' union
  select 102, 'Ireland' union
  select 103, 'Israel' union
  select 104, 'Italy' union
  select 105, 'Jamaica' union
  select 106, 'Japan' union
  select 107, 'Jordan' union
  select 108, 'Kazakstan' union
  select 109, 'Kenya' union
  select 110, 'Kiribati' union
  select 111, 'Korea' union
  select 112, 'Kosovo' union
  select 113, 'Kuwait' union
  select 114, 'Kyrgyzstan' union
  select 115, 'Latvia' union
  select 116, 'Lebanon' union
  select 117, 'Lesotho' union
  select 118, 'Liberia' union
  select 119, 'Libyan Arab Jamahiriya' union
  select 120, 'Liechtenstein' union
  select 121, 'Lithuania' union
  select 122, 'Luxembourg' union
  select 123, 'Macau' union
  select 124, 'Macedonia' union
  select 125, 'Madagascar' union
  select 126, 'Malawi' union
  select 127, 'Malaysia' union
  select 128, 'Maldives' union
  select 129, 'Mali' union
  select 130, 'Malta' union
  select 131, 'Marshall Islands' union
  select 132, 'Martinique' union
  select 133, 'Mauritania' union
  select 134, 'Mauritius' union
  select 135, 'Mayotte' union
  select 136, 'Mexico' union
  select 137, 'Micronesia' union
  select 138, 'Moldova' union
  select 139, 'Monaco' union
  select 140, 'Mongolia' union
  select 141, 'Montserrat' union
  select 142, 'Montenegro' union
  select 143, 'Morocco' union
  select 144, 'Mozambique' union
  select 145, 'Myanmar' union
  select 146, 'Namibia' union
  select 147, 'Nauru' union
  select 148, 'Nepal' union
  select 149, 'Netherlands' union
  select 150, 'Netherlands Antilles' union
  select 151, 'New Caledonia' union
  select 152, 'New Zealand' union
  select 153, 'Nicaragua' union
  select 154, 'Niger' union
  select 155, 'Nigeria' union
  select 156, 'Niue' union
  select 157, 'Norfolk Island' union
  select 158, 'Northern Mariana Islands' union
  select 159, 'Norway' union
  select 160, 'Oman' union
  select 161, 'Pakistan' union
  select 162, 'Palau' union
  select 163, 'Palestina' union
  select 164, 'Panama' union
  select 165, 'Papua New Guinea' union
  select 166, 'Paraguay' union
  select 167, 'Peru' union
  select 168, 'Philippines' union
  select 169, 'Pitcairn' union
  select 170, 'Poland' union
  select 171, 'Portugal' union
  select 172, 'Puerto Rico' union
  select 173, 'Qatar' union
  select 174, 'Reunion' union
  select 175, 'Romania' union
  select 176, 'Russian Federation' union
  select 177, 'Rwanda' union
  select 178, 'Saint Helena' union
  select 179, 'Saint Kitts And Nevis' union
  select 180, 'Saint Lucia' union
  select 181, 'Saint Pierre And Miquelon' union
  select 182, 'Saint Vincent And The Grenadines' union
  select 183, 'Samoa' union
  select 184, 'San Marino' union
  select 185, 'Sao Tome And Principe' union
  select 186, 'Saudi Arabia' union
  select 187, 'Senegal' union
  select 188, 'Serbia' union
  select 189, 'Seychelles' union
  select 190, 'Sierra Leone' union
  select 191, 'Singapore' union
  select 192, 'Slovakia' union
  select 193, 'Slovenia' union
  select 194, 'Solomon Islands' union
  select 195, 'Somalia' union
  select 196, 'South Africa' union
  select 197, 'South Georgia And The South Sandwich Islands' union
  select 198, 'Spain' union
  select 199, 'Sri Lanka' union
  select 200, 'Sudan' union
  select 201, 'Suriname' union
  select 202, 'Svalbard And Jan Mayen' union
  select 203, 'Swaziland' union
  select 204, 'Sweden' union
  select 205, 'Switzerland' union
  select 206, 'Syrian Arab Republic' union
  select 207, 'Taiwan' union
  select 208, 'Tajikistan' union
  select 209, 'Tanzania' union
  select 210, 'Thailand' union
  select 211, 'Togo' union
  select 212, 'Tokelau' union
  select 213, 'Tonga' union
  select 214, 'Trinidad And Tobago' union
  select 215, 'Tunisia' union
  select 216, 'Turkey' union
  select 217, 'Turkmenistan' union
  select 218, 'Turks And Caicos Islands' union
  select 219, 'Tuvalu' union
  select 220, 'Uganda' union
  select 221, 'Ukraine' union
  select 222, 'United Arab Emirates' union
  select 223, 'United Kingdom' union
  select 224, 'United States' union
  select 225, 'United States Minor Outlying Islands' union
  select 226, 'Uruguay' union
  select 227, 'Uzbekistan' union
  select 228, 'Vanuatu' union
  select 229, 'Venezuela' union
  select 230, 'VietNam' union
  select 231, 'Virgin Islands' union
  select 232, 'Wallis And Futuna' union
  select 233, 'Western Sahara' union
  select 234, 'Yemen' union
  select 235, 'Zambia' union
  select 236, 'Zimbabwe'
), 
country_writings as 
(
  select 1 countryid, 'Afghanistan' altname union
  select 2, 'Kabul' union
  select 2, 'Albania' union
  select 2, 'Durres' union
  select 2, 'Tirana' union
  select 3, 'Algeria' union
  select 4, 'American Samoa' union
  select 5, 'Andorra' union
  select 6, 'Angola' union
  select 7, 'Anguilla' union
  select 8, 'Antarctica' union
  select 9, 'Antigua And Barbuda' union
  select 10, 'Argentina' union
  select 11, 'Armenia' union
  select 12, 'Aruba' union
  select 13, 'Australia' union
  select 14, 'Austria' union
  select 15, 'Azerbaijan' union
  select 16, 'Bahamas' union
  select 17, 'Bahrain' union
  select 18, 'Bangladesh' union
  select 19, 'Barbados' union
  select 20, 'Belarus' union
  select 21, 'Belgium' union
  select 22, 'Belize' union
  select 23, 'Benin' union
  select 24, 'Bermuda' union
  select 25, 'Bhutan' union
  select 26, 'Bolivia' union
  select 27, 'Bosnia And Herzegovina' union
  select 28, 'Botswana' union
  select 29, 'Bouvet Island' union
  select 30, 'Brazil' union
  select 31, 'British Indian Ocean Territory' union
  select 32, 'Brunei Darussalam' union
  select 33, 'Bulgaria' union
  select 34, 'Burkina Faso' union
  select 35, 'Burundi' union
  select 36, 'Cambodia' union
  select 37, 'Cameroon' union
  select 38, 'Canada' union
  select 39, 'Cape Verde' union
  select 40, 'Cayman Islands' union
  select 41, 'Central African Republic' union
  select 42, 'Chad' union
  select 43, 'Chile' union
  select 44, 'China' union
  select 45, 'Christmas Island' union
  select 46, 'Cocos Islands' union
  select 47, 'Colombia' union
  select 48, 'Comoros' union
  select 49, 'Congo' union
  select 50, 'Cook Islands' union
  select 51, 'Costa Rica' union
  select 52, 'Cote D''ivoire' union
  select 53, 'Croatia' union
  select 54, 'Cuba' union
  select 55, 'Cyprus' union
  select 56, 'Czech Republic' union
  select 57, 'Denmark' union
  select 58, 'Djibouti' union
  select 59, 'Dominica' union
  select 60, 'Dominican Republic' union
  select 61, 'East Timor' union
  select 62, 'Ecuador' union
  select 63, 'Egypt' union
  select 64, 'El Salvador' union
  select 65, 'Equatorial Guinea' union
  select 66, 'Eritrea' union
  select 67, 'Estonia' union
  select 68, 'Ethiopia' union
  select 69, 'Falkland Islands' union
  select 70, 'Faroe Islands' union
  select 71, 'Fiji' union
  select 72, 'Finland' union
  select 73, 'France' union
  select 74, 'French Guiana' union
  select 75, 'French Polynesia' union
  select 76, 'French Southern Territories' union
  select 77, 'Gabon' union
  select 78, 'Gambia' union
  select 79, 'Georgia' union
  select 80, 'Germany' union
  select 81, 'Ghana' union
  select 82, 'Gibraltar' union
  select 83, 'Greece' union
  select 84, 'Greenland' union
  select 85, 'Grenada' union
  select 86, 'Guadeloupe' union
  select 87, 'Guam' union
  select 88, 'Guatemala' union
  select 89, 'Guinea' union
  select 90, 'Guinea-bissau' union
  select 91, 'Guyana' union
  select 92, 'Haiti' union
  select 93, 'Heard Island And Mcdonald Islands' union
  select 94, 'Honduras' union
  select 95, 'Hong Kong' union
  select 96, 'Hungary' union
  select 97, 'Iceland' union
  select 98, 'India' union
  select 99, 'Indonesia' union
  select 100, 'Iran' union
  select 101, 'Iraq' union
  select 102, 'Ireland' union
  select 103, 'Israel' union
  select 104, 'Italy' union
  select 105, 'Jamaica' union
  select 106, 'Japan' union
  select 107, 'Jordan' union
  select 108, 'Kazakstan' union
  select 109, 'Kenya' union
  select 110, 'Kiribati' union
  select 111, 'Korea' union
  select 112, 'Kosovo' union
  select 113, 'Kuwait' union
  select 114, 'Kyrgyzstan' union
  select 115, 'Latvia' union
  select 116, 'Lebanon' union
  select 117, 'Lesotho' union
  select 118, 'Liberia' union
  select 119, 'Libyan Arab Jamahiriya' union
  select 120, 'Liechtenstein' union
  select 121, 'Lithuania' union
  select 122, 'Luxembourg' union
  select 123, 'Macau' union
  select 124, 'Macedonia' union
  select 125, 'Madagascar' union
  select 126, 'Malawi' union
  select 127, 'Malaysia' union
  select 128, 'Maldives' union
  select 129, 'Mali' union
  select 130, 'Malta' union
  select 131, 'Marshall Islands' union
  select 132, 'Martinique' union
  select 133, 'Mauritania' union
  select 134, 'Mauritius' union
  select 135, 'Mayotte' union
  select 136, 'Mexico' union
  select 137, 'Micronesia' union
  select 138, 'Moldova' union
  select 139, 'Monaco' union
  select 140, 'Mongolia' union
  select 141, 'Montserrat' union
  select 142, 'Montenegro' union
  select 143, 'Morocco' union
  select 144, 'Mozambique' union
  select 145, 'Myanmar' union
  select 146, 'Namibia' union
  select 147, 'Nauru' union
  select 148, 'Nepal' union
  select 149, 'Netherlands' union
  select 150, 'Netherlands Antilles' union
  select 151, 'New Caledonia' union
  select 152, 'New Zealand' union
  select 153, 'Nicaragua' union
  select 154, 'Niger' union
  select 155, 'Nigeria' union
  select 156, 'Niue' union
  select 157, 'Norfolk Island' union
  select 158, 'Northern Mariana Islands' union
  select 159, 'Norway' union
  select 160, 'Oman' union
  select 161, 'Pakistan' union
  select 162, 'Palau' union
  select 163, 'Palestina' union
  select 164, 'Panama' union
  select 165, 'Papua New Guinea' union
  select 166, 'Paraguay' union
  select 167, 'Peru' union
  select 168, 'Philippines' union
  select 169, 'Pitcairn' union
  select 170, 'Poland' union
  select 171, 'Portugal' union
  select 172, 'Puerto Rico' union
  select 173, 'Qatar' union
  select 174, 'Reunion' union
  select 175, 'Romania' union
  select 176, 'Russian Federation' union
  select 177, 'Rwanda' union
  select 178, 'Saint Helena' union
  select 179, 'Saint Kitts And Nevis' union
  select 180, 'Saint Lucia' union
  select 181, 'Saint Pierre And Miquelon' union
  select 182, 'Saint Vincent And The Grenadines' union
  select 183, 'Samoa' union
  select 184, 'San Marino' union
  select 185, 'Sao Tome And Principe' union
  select 186, 'Saudi Arabia' union
  select 187, 'Senegal' union
  select 188, 'Serbia' union
  select 189, 'Seychelles' union
  select 190, 'Sierra Leone' union
  select 191, 'Singapore' union
  select 192, 'Slovakia' union
  select 193, 'Slovenia' union
  select 194, 'Solomon Islands' union
  select 195, 'Somalia' union
  select 196, 'South Africa' union
  select 197, 'South Georgia And The South Sandwich Islands' union
  select 198, 'Spain' union
  select 199, 'Sri Lanka' union
  select 200, 'Sudan' union
  select 201, 'Suriname' union
  select 202, 'Svalbard And Jan Mayen' union
  select 203, 'Swaziland' union
  select 204, 'Sweden' union
  select 205, 'Switzerland' union
  select 206, 'Syrian Arab Republic' union
  select 207, 'Taiwan' union
  select 208, 'Tajikistan' union
  select 209, 'Tanzania' union
  select 210, 'Thailand' union
  select 211, 'Togo' union
  select 212, 'Tokelau' union
  select 213, 'Tonga' union
  select 214, 'Trinidad And Tobago' union
  select 215, 'Tunisia' union
  select 216, 'Turkey' union
  select 217, 'Turkmenistan' union
  select 218, 'Turks And Caicos Islands' union
  select 219, 'Tuvalu' union
  select 220, 'Uganda' union
  select 221, 'Ukraine' union
  select 222, 'United Arab Emirates' union
  select 223, 'United Kingdom' union
  select 224, 'United States' union
  select 225, 'United States Minor Outlying Islands' union
  select 226, 'Uruguay' union
  select 227, 'Uzbekistan' union
  select 228, 'Vanuatu' union
  select 229, 'Venezuela' union
  select 230, 'VietNam' union
  select 231, 'Virgin Islands' union
  select 232, 'Wallis And Futuna' union
  select 233, 'Western Sahara' union
  select 234, 'Yemen' union
  select 235, 'Zambia' union
  select 236, 'Zimbabwe' union
  select 10, 'Buenos Aires' union  
  select 13, 'Canberra' union
  select 13, 'Sydney' union
  select 14, 'Österreich' union
  select 14, 'Vienna' union
  select 14, 'Wien' union
  select 14, 'Graz' union
  select 14, 'Linz' union
  select 14, 'Salzburg' union  
  select 38, 'Nova Scotia' union
  select 38, 'London, ON' union
  select 38, 'Ontario' union
  select 38, 'Toronto' union
  select 38, 'Vancouver' union
  select 38, 'Waterloo, ON' union
  select 56, 'Prague' union
  select 56, 'Praha' union
  select 72, 'Suomi' union
  select 72, 'Helsinki' union
  select 72, 'Kuopio' union
  select 72, 'Espoo' union 
  select 72, 'Jyväskylä' union
  select 72, 'Tampere' union
  select 72, 'Turku' union
  select 73, 'Paris' union
  select 73, 'Lyon' union
  select 79, 'Tbilisi' union
  select 80, 'Berlin' union
  select 80, 'Freiburg' union
  select 83, 'Ελλάδα' union
  select 83, 'Athens' union
  select 98, 'Bangalore' union
  select 98, 'Delhi' union
  select 98, 'Kolkata' union
  select 98, 'Mumbai' union
  select 102, 'Dublin' union
  select 103, 'Haifa' union
  select 103, 'Tel Aviv' union
  select 103, 'Tel-Aviv' union
  select 104, 'Bergamo' union
  select 104, 'Milan' union
  select 104, 'Naples' union
  select 104, 'Napoli' union
  select 104, 'Rome' union   
  select 104, 'Torino' union
  select 104, 'Turin' union
  select 106, 'Osaka' union
  select 106, 'Tokyo' union
  select 106, 'Yokohama' union
  select 109, 'Nairobi' union
  select 116, 'Beirut' union
  select 159, 'Alversund' union
  select 159, 'Bergen' union
  select 159, 'Lillehammer' union
  select 170, 'Dolny Slask' union
  select 170, 'Warsaw' union
  select 170, 'Wroclaw' union
  select 186, 'Jeddah' union
  select 192, 'Banovce' union
  select 192, 'Banska Bystrica' union
  select 192, 'Banska Stiavnica' union
  select 192, 'Bardejov' union
  select 192, 'Bojnicece' union
  select 192, 'Bratislava' union
  select 192, 'Brezova pod' union
  select 192, 'Brezno' union
  select 192, 'Bytca' union
  select 192, 'Cadca' union
  select 192, 'Chorvatsky Grob' union
  select 192, 'Cifer' union
  select 192, 'Detva' union
  select 192, 'Dolny Kubin' union
  select 192, 'Dunajska Streda' union
  select 192, 'Filakovo' union
  select 192, 'Gabcikovo' union
  select 192, 'Galanta' union
  select 192, 'Gbely' union
  select 192, 'Gelnica' union
  select 192, 'Giraltovce' union
  select 192, 'Komarno' union
  select 192, 'Komarom' union
  select 192, 'Malacky' union
  select 192, 'Pozsony' union
  select 192, 'Rimaszombat' union
  select 192, 'Rimavska Sobota' union
  select 192, 'Szlovakia' union
  select 193, 'Ljubljana' union
  select 204, 'Göteborg' union  
  select 204, 'Gothenburg' union  
  select 204, 'Stockholm' union  
  select 206, 'Syria' union
  select 206, 'Damascus' union
  select 221, 'Cherkassy' union  
  select 221, 'Kyiv' union
  select 221, 'Lviv' union
  select 223, 'London' union
  select 223, 'England' union
  select 223, 'Scotland' union
  select 223, 'Lancaster' union
  select 223, 'Oxford' union
  select 223, 'Northampton' union
  select 223, 'Watford' union
  select 224, 'USA' union
  select 224, 'Albany, CA' union
  select 224, 'Albany, NY' union
  select 224, 'Albany, OR' union
  select 224, 'Ann Arbor' union
  select 224, 'Athens, GA' union 
  select 224, 'Athens, OH' union 
  select 224, 'Atlanta' union   
  select 224, 'Austin' union 
  select 224, 'Boston' union 
  select 224, 'Boxford, MA' union 
  select 224, 'Boxford MA' union 
  select 224, 'California' union 
  select 224, 'Chicago' union 
  select 224, 'Cleveland' union 
  select 224, 'Damascus, AR' union
  select 224, 'Damascus, MD' union
  select 224, 'Damascus, OR' union
  select 224, 'Damascus, VA' union
  select 224, 'Florida' union
  select 224, 'Indiana' union 
  select 224, 'Lancaster, PA' union
  select 224, 'Lancaster, Pennsylvania' union
  select 224, 'Lebanon, IL' union
  select 224, 'Lebanon, MO' union
  select 224, 'Lebanon, NH' union
  select 224, 'Lebanon, NJ' union
  select 224, 'Lebanon, PA' union
  select 224, 'Lebanon, TN' union
  select 224, 'Jacksonville' union 
  select 224, 'Kennewick, WA' union 
  select 224, 'Massachusetts' union 
  select 224, 'Minneapolis' union 
  select 224, 'Northampton, MA' union
  select 224, 'Los Angeles' union
  select 224, 'New Jersey' union
  select 224, 'New York' union
  select 224, 'Pennsylvania' union
  select 224, 'Pittsburgh' union
  select 224, 'Phoenix, AZ' union
  select 224, 'Phoenixville' union
  select 224, 'Portland' union
  select 224, 'San Diego' union
  select 224, 'San Francisco' union
  select 224, 'Seattle' union
  select 224, 'Socorro, NM' union
  select 224, 'Texas' union
  select 224, 'Urbana' union
  select 224, 'Vancouver, WA' union
  select 224, 'Vancouver, Washington' union
  select 224, 'Waterloo, AL' union
  select 224, 'Waterloo, IA' union
  select 224, 'West Hills'
), 
exclude_writings as 
(
  select 23 countryid, 'Bening' altname union
  select 38, 'Belgium' union 
  select 38, 'Vancouver, WA' union
  select 38, 'Vancouver, Washington' union
  select 38, 'Waterloo, AL' union
  select 38, 'Waterloo, IA' union
  select 54, 'Cubal' union
  select 54, 'Cubase' union
  select 98, 'indiana' union  
  select 79, 'united states' union
  select 79, 'USA' union
  select 79, 'U.S.' union
  select 79, 'US' union
  select 79, ' GA' union
  select 79, 'atlanta' union
  select 79, 'german' union
  select 83, 'Athens, GA' union
  select 83, 'Athens, OH' union
  select 83, 'Greecela' union
  select 83, 'Greecemania' union
  select 116, 'Lebanon, IL' union
  select 116, 'Lebanon, MO' union
  select 116, 'Lebanon, NH' union
  select 116, 'Lebanon, NJ' union
  select 116, 'Lebanon, PA' union
  select 116, 'Lebanon, TN' union
  select 192, 'TN,' union
  select 192, 'USA' union
  select 206, 'Damascus, AR' union
  select 206, 'Damascus, MD' union
  select 206, 'Damascus, OR' union
  select 206, 'Damascus, VA' union
  select 206, 'USA' union
  select 206, 'United' union
  select 223, 'UAE' union
  select 223, 'Abu Dhabi' union
  select 223, 'Dubai' union
  select 223, 'Boxford, MA' union 
  select 223, 'Lancaster, PA' union
  select 223, 'Lancaster, Pennsylvania' union
  select 223, 'London, Canada' union
  select 223, 'London, ON' union
  select 223, 'London, Ontario' union  
  select 223, 'Northampton, MA' 
),
users_and_country as
(
  select distinct 
         cw.countryid
       , u.id
       , u.reputation
       , u.location
  from #users u
  inner join country_writings cw 
     on (   u.location = cw.altname COLLATE Latin1_General_CI_AI
         or u.location like concat('%', cw.altname, '%') COLLATE Latin1_General_CI_AI
        )
  where not exists (
    select *
    from exclude_writings ew
    where ew.countryid = cw.countryid
    and u.location like concat('%', ew.altname, '%') COLLATE Latin1_General_CI_AI
  ) 
)

select c.name, data.id as [User Link], reputation, location
from countries c
inner join
(
select rank() over(partition by countryid order by reputation desc) [pos]
     , *
from users_and_country 
) data on c.id = countryid 
      and pos = 1 -- only the top user for a country

ORDER BY
    Name ASC

Enter Parameters

Options:
Switch to main site
loading Hold tight while we fetch your results