MySQL GROUP BY - get SUM of few grouped values - mysql

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.

Related

Finding Max of Max mysql

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;

Average values of the top values for different categories

I have a table with 3 fields, touristic places, the country they're in and the average rating by tourists for this place. I would like to compare different countries based on the average rating of their top touristic places. I use MySQL
It looks like this basically :
Eiffel Tower | France | 4,2
Trakoscan Castle | Croatia | 4,6
For example, how does the average of the notes of the 5 best touristic places in France compare with the average of the notes of the 5 best touristic places in Croatia. I know how to average all places for a country and compare that but I don't know how to combine LIMIT and GROUP BY
Thank you for your help.
You can use window functions to filter on the top 5 notes per country, then aggregate.
Assuming that your table has columns country, place and rating, you would phrase the query as:
select country, avg(rating) avg_rating_top5
from (
select t.*,
row_number() over(partition by country order by rating desc) rn
from mytable t
) t
where rn <= 5
group by country
Note that window functions are available in MySQL 8.0 only.

How to find top N records from 'group by' method where N records can contain multiple 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

Calculating rank not working - Mysql

I have a DB Table user_points which contains user's points and I am trying to calculate ranking based on points. It is working fine for all users except users having 1 point.
If user have 1 point it is showing it's rank as 0 but it should display it's rank as last or in last numbers like: 12083 etc.
Higher points are, ranking should be higher as well. For example:
1000 points = rank 1
1 point = rank 1223
Following is the query.
SELECT id, mobileNo, points,
FIND_IN_SET( points, (SELECT GROUP_CONCAT( points ORDER BY points DESC )
FROM users_points )) AS rank
FROM users_points
WHERE mobileNo = '03214701777'
What should I change to fix it?
SELECT a.id, a.mobileNo, a.points,
IFNULL((SELECT COUNT(*) AS rank
FROM users_points b
WHERE b.points<a.points), 0)+1 as rank
FROM user_points a
WHERE a.mobileNo = '03214701777'
Seems to be what you are looking for. While it is still very innefficient it is better than your approach using FIND_IN_SET(). If you really want to use FIND_IN_SET() then you need to pad the scores to a consistent width and divide by the width+1 to get the rank.

How to Count Distinct with Group By and the entire Column

I'm having a hard time wording what I need/wording the search result, so apologies if this is a stupid question/has been answered before. I'm trying to write a query in SQL for a table such as below:
Country Unique_ID
US 123
US 124
UK 125
Australia 126
That will output the follow table:
Country Count_Distinct
US 2
UK 1
Australia 1
All 4
I know I can select the countryid and count distinct the country codes, and I know I can just count distinct the countryid codes to get the "All" number. I can't figure out how to write a query to get the follow output that's not two separate queries.
If you need information or clarification please let me know. Thanks!
Use WITH ROLLUP:
select Country, count(distinct Unique_ID) Count_Distinct
from mytable
group by Country
with rollup
If you want the text "All" (you get a null for the country by default), wrap it in another query to change the null to "All":
select coalesce(Country, "All") Country, Count_Distinct
from (
select Country, count(distinct Unique_ID) Count_Distinct
from mytable
group by Country
with rollup
) x
Can you please try this :
select country,count(distinct unique_id) as count distinct
from table
group by rollup(country)