How can i find the maximum gdp per population in sql? - mysql

I have 2 tables countries and coutry_stats.
Countries table
Country_id
name
country_code
1
Aruba
AW
2
Afghanistan
AF
Country_stats table
Country_id
year
population
gdp
1
1986
62644
405463417
1
1987
61833
487602457
1
1988
61079
596423607
1
1989
61032
695304363
1
1990
62149
764887117
1
1991
64622
872138730
2
1960
8996973
537777811
2
1961
9169410
548888895
2
1962
9351441
546666677
2
1963
9543205
751111191
2
1964
9744781
800000044
2
1965
9956320
1006666638
How can i find the maximum gdp / popupation along the years?
i tried
select
countries.country_id,
name,
country_code,
year,
gdp,
population,
max(gdp / population) as per_ratio
from
countries JOIN country_stats on
countries.country_id = country_stats.country_id
where
countries.country_id = 1 group by name order by per_ratio;
but im getting this
Country_id
name
country_code
year
gdp
population
per_ratio
1
Aruba
AB
1986
405463417
62644
27084.7037
the per_ratio is the correct maximum of this country but this is not the correct record in the database.

This would likely yield the result you're looking for - it sorts per_ratio DESC and limits that record to one.
SELECT
countries.country_id,
name,
country_code,
year,
gdp,
population,
gdp / population as per_ratio
FROM countries
JOIN country_stats
on countries.country_id = country_stats.country_id
WHERE countries.country_id = 1
ORDER BY per_ratio DESC
LIMIT 1;
Alternatively, you could use a partition to first get the max for the country id in a CTE, and then pull in all records where max_per_ratio = per_ratio
WITH MAX_PER AS(
SELECT
countries.country_id,
name,
country_code,
year,
gdp,
population,
gdp / population as per_ratio,
MAX(gdp/population) OVER(PARTITION BY country_id) AS max_per_ratio
FROM countries
JOIN country_stats
on countries.country_id = country_stats.country_id
WHERE countries.country_id = 1)
SELECT
country_id,
name,
country_code,
year,
gdp,
population,
per_ratio
FROM MAX_PER
WHERE max_per_ratio = per_ratio;

Related

Bigquery SQL - List the top vendors in each country, for each year in the dataset

I need to join 2 tables and extract the top vendor by country by year.
The original ORDERS table looks like this :
country_name date_local vendor_id gmv_local
Taiwan 2012-10-02 2870 559.6
Taiwan 2012-10-02 3812 573.5
Singapore 2012-10-02 941 778.6
Singapore 2014-10-02 13 120.6
Thailand 2014-10-02 227 563.6
This table is merged with the vendor table
id vendor_name country_name
2870 A House Taiwan
941 B House Singapore
227 C House Thailand
I would like to extract the year from the "date_local" column into timestamp format, where the items in the Year column will look like "2012-01-01T00:00:00" from the original date format of "2012-10-02"
Then I would like to list out the top 2 vendors by year for each country in total gmv.
The resulting table should look like this:
year country_name vendor_name total_gmv
2012-01-01T00:00:00 Singapore A House 1119.76
2012-01-01T00:00:00 Singapore B House 819.63
2012-01-01T00:00:00 Taiwan C House 119.6
2012-01-01T00:00:00 Taiwan D House 119.6
2012-01-01T00:00:00 Bangkok 9 House 119.6
2014-01-01T00:00:00 Singapore A House 2119.76
2014-01-01T00:00:00 Singapore B House 1819.63
2014-01-01T00:00:00 Taiwan C House 1019.6
2014-01-01T00:00:00 Taiwan D House 919.6
2014-01-01T00:00:00 Bangkok 9 House 189.6
Based on, among others, some previous guidance here, I've come up with the following query:
SELECT
Ord.country_name,
vn.vendor_name,
EXTRACT(year FROM date_local) AS year,
ROUND(SUM(Ord.gmv_local), 2) AS total_gmv
FROM ORDERS AS Ord
LEFT JOIN `primeval-falcon-306603.foodpanda_BI_Exercise.Vendors` AS vn
ON Ord.vendor_id = vn.id
GROUP BY
Ord.country_name,
vn.vendor_name,
EXTRACT(year FROM date_local)
QUALIFY ROW_NUMBER() OVER (PARTITION BY country_name, EXTRACT(year FROM date_local)
ORDER BY total_gmv DESC) <= 2
ORDER BY
Ord.country_name DESC,
total_gmv DESC;
But keep getting this error message: SELECT list expression references column date_local which is neither grouped nor aggregated at [4:23]
So unfortunately not even able to see if I'm getting the result I'm looking for.
Any advice would be much appreciated
with ORDERS (country_name,date_local,vendor_id,gmv_local) as(
select 'Taiwan', Date('2012-10-02'), 2870 , 559.6 union all
select 'Taiwan', Date('2012-10-02'), 3812, 573.5 union all
select 'Taiwan', Date('2012-10-02'), 3813, 555.5 union all
select 'Singapore', Date('2012-10-02'), 941, 778.6 union all
select 'Singapore', Date('2014-10-02'), 13, 120.6 union all
select 'Thailand', Date('2014-10-02'), 227, 563.6
)
,vendor(id, vendor_name, country_name) as(
select 2870, 'A House','Taiwan' union all
select 3812, 'D House','Taiwan' union all
select 3813, 'F House','Taiwan' union all
select 941, 'B House','Singapore' union all
select 13, 'E House','Singapore' union all
select 227, 'C House','Thailand'
)
select country_name,vendor_name,year,total_gmv from(
SELECT
Ord.country_name,
vn.vendor_name,
EXTRACT(year FROM date_local) AS year,
ROUND(SUM(Ord.gmv_local), 2) AS total_gmv,
ROW_NUMBER() OVER (PARTITION BY country_name,EXTRACT(year FROM date_local)) rn
FROM ORDERS AS Ord
LEFT JOIN vendor AS vn
ON Ord.vendor_id = vn.id
GROUP BY
Ord.country_name,
vn.vendor_name,
EXTRACT(year FROM date_local)
ORDER BY
Ord.country_name DESC,
total_gmv DESC
)a where a.rn <= 2

find maximum count in sql

I have a table containing a sample data of covid variant cases
country covid_variant cases
USA SARS Covid 2000
USA Delta 100
USA Omicron 500
Mexico SARS Covid 2000
USA Omicron 400
How can I get the data based on max cases of each variant?
covid_variant countries max_cases
SARS Covid USA 2000
SARS Covid Mexico 2000
Delta USA 100
Omicron USA 500
Using ROW_NUMBER:
WITH cte AS (
SELECT *, ROW_NUMBER() OVER (PARTITION BY covid_variant, country
ORDER BY cases DESC) rn
FROM yourTable
)
SELECT covid_variant, country, cases
FROM cte
WHERE rn = 1;
Supposing this is MSSQL, with TSQL syntax, one possible answer is:
select covid_variant, country, MAX(cases) as max_cases
from [tablename]
group by covid_variant, country
order by covid_variant, country
If your using MySQL 5.7 try using MAX and GROUP BY.
SELECT covid_variant,
country,
MAX(cases) AS max_cases
FROM your_table
GROUP BY covid_variant, country
ORDER BY max_cases DESC, country DESC;
RESULT
covid_variant country max_cases
------------- ------- -----------
SARS Covid USA 2000
SARS Covid Mexico 2000
Omicron USA 500
Delta USA 100

Find MAX(Avg(value)) after grouped by two fields

The table name is c_list.
Country City Rating Date
------------------------------------
France Brest 95 24092016
France Brest 98 27092016
France Brest 95 03102016
France Lille 100 26092016
France Lille 92 28092016
Japan Tokyo 98 02102016
There are more than 50 different countries and each country have few cities. And each city may have more than one row of record or more. I want to select one City with highest average Rating (Compare to Cities in it's own country) and then compare with all other cities in different countries. So, the final query should display all Country and their ONE City with max(avg(Rating)) and in desc order. The sample output:
Country City max(avg(rating))
-------------------------------------
USA New York 97.25
UK Cardiff 96.70
Germany Greven 96.50
Turkey Afyon 94.88
France Guipavas 94.10
Canada Cartwright 91.35
I can only get the max(avg(rating)) for one country. Need help.
SELECT top 1 country, city, Avg(rating) AS Ratings
FROM c_list
where country = 'France'
GROUP BY city, country
order by Ratings desc
(Edited) The result that I want is similar like Miss world contest. Compete and win against local contestant in your country first. Next (my final result set) is to compete against the winners from other countries and rank them first to last using their avg(rating) that they got eatlier in their country.
If am not wrong you are looking for this
SELECT country,
city,
Avg(rating) AS Ratings
FROM c_list A
GROUP BY city,
country
HAVING Avg(rating) = (SELECT TOP 1 Avg(rating) AS Ratings
FROM c_list B
WHERE a.country = b.country
GROUP BY city
ORDER BY ratings DESC)
ORDER BY ratings DESC
Note : If you are using Mysql the replace TOP keyword with LIMIT
Base table:
select t.* from temp_lax t
COUNTRY CITY RATING
1 France Brest 95
2 France Brest 98
3 France Brest 95
4 France Lille 100
5 France Lille 92
6 Japan Tokyo 98
Query:
select t1.country, t1.city, Avg(t1.Rating) rating
from temp_lax t1
group by t1.country, t1.city
having avg(t1.rating) = (select max(avg(rating))
from temp_lax t2
WHERE t1.country = t2.country
GROUP BY t2.city)
order by rating desc
OUTPUT:
COUNTRY CITY RATING
1 Japan Tokyo 98
2 France Lille 96
3 France Brest 96
Please let me know if you are looking for different result set.

Displaying the value with the most occurrences

I have two related tables:
(1) people contains names and image files.
(2) cities contains cities they have visited.
people
id name image
1 John NULL
2 Carrie 001.jpg
3 Desmond 002.jpg
4 Harry 003.jpg
5 Paul NULL
cities
id city people_id year_visited
1 Chicago 1 2000
2 Chicago 4 2000
3 Chicago 5 2001
4 Paris 1 2000
5 Paris 2 2002
6 Chicago 4 2002
7 Chicago 1 2001
8 London 1 2004
9 Sydney 5 2001
10 Sydney 1 2002
11 Rio 5 2002
12 London 5 2004
13 Sydney 5 2003
14 Sydney 5 2005
I would like to identify all people without an image, and the city they have visited the most. So the results I am looking for is:
name most_visited_city number_of_visits
John Chicago 2
Paul Sydney 3
I can group_concat the cities they have visited, but not drill down to the single city they visited the most.
All help gratefully appreciated.
The following gets people, cities, and the count:
select p.id, c.city, count(*) as cnt
from people p join
cities c
on p.id = c.people_id
where p.image is null
group by p.id, c.city;
Getting information about the most visited is tricky in MySQL. Here is one method that works if the data is not too large:
select id,
substring_index(group_concat(city order by cnt desc separator '|'), '|', 1) as most_visited_city,
max(cnt) as number_of_times_visited
from (select p.id, c.city, count(*) as cnt
from people p join
cities c
on p.id = c.people_id
where p.image is null
group by p.id, c.city
) pc
group by id;
This query should return the most visited city for each people_id in cities.
SELECT t1.people_id, t2.city, t2.visits
FROM (
SELECT people_id, MAX(visits) AS max_visits
FROM (
SELECT people_id, city, COUNT(*) AS visits
FROM cities
GROUP BY people_id, city) x
GROUP BY people_id) AS t1
JOIN (
SELECT people_id, city, COUNT(*) AS visits
FROM cities
GROUP BY people_id, city) AS t2
ON t1.people_id = t2.people_id AND t1.max_visits = t2.visits
The general structure is based on an answer in SQL Select only rows with Max Value on a Column, but instead of getting the max value of a column in the table, it's using the max value in the subquery that counts visits per city. Unfortunately, it results in an ugly query because you have to repeat that subquery, since MySQL doesn't have CTEs.
Then you can join it with people to get the person's name and filter out the ones with an image.
SELECT p.name, t2.city, t2.visits
FROM (
SELECT people_id, MAX(visits) AS max_visits
FROM (
SELECT people_id, city, COUNT(*) AS visits
GROUP BY people_id, city) x
GROUP BY people_id) AS t1
JOIN (
SELECT people_id, city, COUNT(*) AS visits
GROUP BY people_id, city) AS t2
ON t1.people_id = t2.people_id AND t1.max_visits = t2.max_visits
JOIN people AS p ON p.id = t1.people_id
WHERE p.image IS NULL
DEMO

selecting top row of each repeating tuple according to some criteria (MYSQL)

I have an example table like this:
Month City Person
8 LHR ABC
10 BEIJING BCS
11 NY JJJ
11 VENICE hghg
11 VENICE KKK
12 NY aa
12 ORL abc
12 ORL bbc
So what I want to achieve is see the city in a specific month with the most number of people visiting
Like the output should be:
12 ORL
11 VENINCE
10 BEIJING
8 LHR
I have tried grouping it like
SELECT month, city , count(*) AS 'no of people visiting'
FROM table
GROUP BY month, city
This table does tell me which city and month location had how many people
visiting but I cannot extract the the top most month and city combination
with respect to a certain month.
Updated Query (with error)
SELECT *
FROM
( SELECT monthname(reservation.PickupDate), location.LocationName, COUNT(*) AS count
FROM reservation NATURAL JOIN location
WHERE reservation.pickupdate >= DATE_ADD(NOW(), INTERVAL - 3 MONTH)
GROUP BY month(reservation.PickupDate), location.LocationName) AS t1
WHERE NOT EXISTS (SELECT 1
FROM reservation R1 NATURAL JOIN location L1
WHERE monthname(R1.PickupDate) = monthname(t1.PickupDate)
GROUP BY month(R1.PickupDate), L1.LocationName)
Starting from your query, you just need to eliminate those rows having another city with more visitors on that month:
SELECT *
FROM
(SELECT `month`, city, count(*) AS cnt
FROM `table`
GROUP BY `month`, city) t1
WHERE NOT EXISTS(SELECT 1
FROM `table` t2
WHERE t2.`month` = t1.`month`
GROUP BY `month`, city
HAVING count(*) > t1.cnt)