I am using a table called covid_vaccinations.
To briefly explain about the table, it tracks down all the countries' vaccination completion by every single day from Feb xx, 2020 to Jan XX, 2022.
The name of the countries are called 'location' in this table.
The countries (location) are also categorized in the column of 'continent'
To find the people who are fully vaccinated in Asia, I used the query below:
SELECT continent,location, MAX(people_fully_vaccinated)
FROM covid_vaccinations
WHERE continent LIKE '%ASIA%'
GROUP BY continent, location
ORDER BY 3 DESC;
I used MAX() since the <people_fully_vaccinated> column includes the cumulative number of data.
The query above gave me the result I wanted, see <image 1>
HERE IS MY QUESTION:
If I just want to get the GREATEST result of people_fully_vaccinated, how should I write the query?
I tried below, and it gave me the same result as <image 1>
SELECT location, MAX(peep_f_vacc_asia)
FROM (
SELECT location, MAX(people_fully_vaccinated) as peep_f_vacc_asia
FROM covid_vaccinations
WHERE continent LIKE '%ASIA%'
GROUP BY continent,location
) A
GROUP BY location
ORDER BY 2 DESC;
The desired result I want to see would be only a single row, China (which has the greatest number of people_fully_vaccinated)
Thank you so much guys...
You might be able to get away with just using a LIMIT query. A slight modification of your first query:
SELECT continent, location, MAX(people_fully_vaccinated)
FROM covid_vaccinations
WHERE continent LIKE '%ASIA%'
GROUP BY continent, location
ORDER BY 3 DESC
LIMIT 1;
But this only works in the case that there are no ties for a given continent and location for the max number of fully vaccinated. If you do have to worry about ties, and you are using MySQL 8+, then we can use RANK as follows:
WITH cte AS (
SELECT continent, location, MAX(people_fully_vaccinated) AS max_fv,
RANK() OVER (ORDER BY MAX(people_fully_vaccinated) DESC) rnk
FROM covid_vaccinations
WHERE continent LIKE '%ASIA%'
GROUP BY continent, location
)
SELECT continent, location, max_fv
FROM cte
WHERE rnk = 1;
Related
I have a simple db where I have users and every user have 'country', for ex:
Dmitry - US
Ann - US
John - UK
Roman - Japan
Mila - China
Jane - Australia
I want to get count of very country users, BUT I need to get TOP 3 countries users counts (US, UK, Japan for example), and all other countries users count should be summarized together as "Rest". How to do this?
So in my example this should give me this result from SQL:
US = 2
UK = 1
Japan = 1
Rest = 2
If I will make regular SQL:
SELECT count(userid) FROM users GROUP BY country
I will get results for every country, but I need only TOP 3 and all others count as "Rest" in results. Thanks!
P.S.: I tried to create SQLFiddle for this, but their website is down right now and I can't use it.
You can group by country and use ROW_NUMBER() window function to rank the countries based on the number of times they appear.
Then add another level of aggregation based on the ranking position of each country:
SELECT CASE WHEN rn <= 3 THEN country ELSE 'Rest' END country,
SUM(counter) counter
FROM (
SELECT country, COUNT(*) counter,
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC) rn
FROM users
GROUP BY country
) t
GROUP BY 1;
Note that the countries returned as top 3 in case of ties may be arbitrary chosen, so you could add another condition in the ORDER BY clause of ROW_NUMBER(), like:
ROW_NUMBER() OVER (ORDER BY COUNT(*) DESC, country)
which would return different but consistent results.
See the demo.
I have a kinda peculiar query to run. I need to SUM the population value of different prefectures under each region as a column, and return it to the main query. For example this query:
SELECT region_en
, population AS temppop
FROM prefectures
WHERE region_id = 12
GROUP
BY region_en
returns this table:
Karditsa 129541
Larissa 279305
Magnesia 206995
Sporades 13798
Trikala 138047
All the above belongs to the same region id (12), and i need to get the SUM of all those populations under the same query. I tried applying the above but it is not working. I dont get the sum which is 767686 but 95960750 instead:
SELECT SUM(b.cases) as cases
, COALESCE(SUM(o.poptemp), 0) as pop
FROM prefectures AS b
LEFT
JOIN
( SELECT region_en
, population AS poptemp
FROM prefectures
WHERE region_id = 12
GROUP
BY region_en
) AS o
ON o.region_en = b.region_en
WHERE b.region_id = '12'
Basically I need the total Cases per region, as well as the sum of all people living under it.
You seem to be looking for a window sum. Your original query is not a valid aggregation query, which makes things a little unclear.
If there is just one row per region_en, then no need to aggregate:
SELECT region_en, population, SUM(population) OVER() as region_population
FROM prefectures
WHERE region_id = 12
You can get the same result for all regions at once like so:
SELECT region_en, population,
SUM(population) OVER(PARTITION BY region_id) AS region_population
FROM prefectures
If there really are several rows per region_en:
SELECT region_en, SUM(population) AS population,
SUM(SUM(population)) OVER(PARTITION BY region_id) as region_population
FROM prefectures
GROUP BY region_id, region_en
Note that window functions are available in MySQL 8.0 only. In earlier versions, you would phrase the query as:
SELECT region_en, population,
(SELECT SUM(p1.population) FROM prefectures p1 WHERE p1.region_id = p.region_id) AS region_population
FROM prefectures p
Actually i used a different approach.
Select SUM(o.cas) as cases, SUM(o.pop) as population FROM
(SELECT a.population as pop, SUM(a.cases) as cas FROM prefectures a WHERE a.region_id = 12 GROUP BY a.region_en) AS o
This brings back the correct values and it is superfast as well.
In the end you get 2 columns:
cases population
2515 767686
which is the correct values.
I was practicing with W3 School SQL Tables. From Customers table I can select Number of Customers Per country by following SQL
select country, count(*) as NumOfCustomer
from Customers
group by country
order by NumOfCustomer Desc;
This gives me a result like this:
If I want to select top 5 countries with most customers I can not use Limit 5 because Germany and France have second most highest customers and Mexico and Spain have fifth most highest customers.Using Limit 5 won't include Mexico and Spain
How can I get a result containing all N number of highest values where highest values can be repeated for a Nth Number like before?
You could use DENSE_RANK:
Returns the rank of the current row within its partition, without gaps. Peers are considered ties and receive the same rank. This function assigns consecutive ranks to peer groups; the result is that groups of size greater than one do not produce noncontiguous rank numbers
WITH cte AS (
select country, count(*) as NumOfCustomer
from Customers
group by country
), cte2 AS (
SELECT *, DENSE_RANK() OVER(ORDER BY NumOfCustomer DESC) AS rnk
FROM cte
)
SELECT *
FROM cte2
WHERE rnk <= 5
ORDER BY NumOfCustomer DESC
I'm wondering how one would sum the results from a query?
I want to know how many people live in total in the three biggest cities in Norway. I'm using mysql, the world.sql sample database in mysql workbench.
This is the closest I've gotten
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
There's a few problems here namely this gives me three results instead of one, and while using LIMIT which actually limits how many results it gives, not how many it uses.
Any ideas?
You would use a subquery:
SELECT SUM(population)
FROM (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
) cp
simply sum the result:
select sum(population) from (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) t1
select sum(population) from (SELECT population FROM city WHERE
CountryCode = 'NOR' ORDER BY population DESC LIMIT 3) temp
Read on subqueries.
Make your current query a subquery and get sum from your subquery.
SELECT SUM(population) FROM (
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) p
You query will now act as a virtual table, from which you can you can write a select query to get the sum
So i need to select the city with highest and lowest population in the same query using a Union statement... I have tried almost every answer on the site, google, mysql.com and mysql tutorials... nothing works so
this is my code
(select name, population from city order by population asc limit 1)
union
( select name, population from city order by population desc limit 1)
it is not working obviously, I dont want the answer, I just need to understand why it isnt working and what to fix please and thanks