MYSQL Calculate total and average based on another column - mysql

How would I go about calculating the average and total Bonus from each City?
Example Input
Toronto 5
Toronto 8
New York 7
New York 3
London 10
Desired Output:
City Avg Total
Toronto 6.5 13
New York 5 10
London 10 10

This is a very simple group by statement:
SELECT City, AVG(Bonus) as `Avg`, SUM(Bonus) AS Total
FROM Cities
GROUP BY City
See example sqlfiddle here with your expected results.

You can do something like this:
SELECT city, AVG(bonus), SUM(bonus) FROM city_bonus GROUP BY city
Working fiddle

Related

MySql Numbering the rows of query based on group by

I have a table like this
id name city
-------------------------------
1 Ian London
2 John London
3 David New York
4 Sylvia Mumbai
5 Beryl New York
6 Rashan London
I would like to retrieve the data with a row numbering that is grouped on City. Like this.
name city count
-------------------------------
Ian London 0
John London 1
Rashan London 2
Beryl New York 0
David New York 1
Sylvia Mumbai 0
I have been trying with
ROW_NUMBER() OVER (PARTITION BY City ORDER BY name)-1
But the count is of all the items returned, where I would like to count the number of people in each city.
Your code should do what you want - however, you mention group by, which is irrelevant here. The query should just be:
select name, city, row_number() over(partition by city order by name) - 1 rn
from mytable
To get the results that you specify:
select name, city, row_number() over (partition by city, order by name) as count
from t
order by count(*) over (partition by city) desc, -- number of rows in city
city,
name;
You seem to want the cities ordered by the number of names in the city.

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.

How to count the number of rows matching certain values?

Actually I have a table now:
[Person Table]:
ID Name age City
====================================
1 Jack 14 New York
2 Mike 15 LA
3 Ben 16 Beijing
?
100 Lee 32 Singapore
(total record = 100)
(Id is Primary Key)
Please provide a SQL script to query customer that his/her city occurs in the table more than or equal to 6.
Example:
The number of customer that live in New York is 10
The number of customer that live in LA=5
The number of customer that live in Beijing=6.
So in this example the output should be all customer that live in New York and Beijing only.
Something like this should work:
select * from customers
join (select city from customers group by city having count(*) >= 6)
as city_count
on customers.city = city_count.city;
All you are doing is creating a list of the cities that have six or more customers and then using that to filter the original customers table.
Link to SQL Fiddle - using 2 as the threshold

List SQL data by a count of a group

I hope you can help me with an SQL statment that I can't seem to figure out.
I have a table with a list of visits made, with the country visited and the dates such as;
United Kingdom, 1st Nov 2009
Germany, 8th June 2010
Frane, 10th September 2011
United Kingdom, 11th october 2011
etc.
I want to extract the data so that I get a table list such as follows
Times Visited - Country list
23 - United Kingdom
10 - France, Germany
4 - Czech Republic, USA, Canada
1 - Poland, Serbia, Argentina, New Zealand
So that the data shows that I have made 4 visist to the Czech Republic, USA & Canda
And the query
select * from
(
select count(*) as "trips", country from trip_view
group by country
order by 1 desc
) as a
returns the data
23 United Kingdom
10 France
10 Germany
4 Czech Republic
4 USA
4 Canada
etc.
So I need a kind of group on the outer SQL, but if I do the following
select * from
(
select count(*) as "trips", country from trip_view
group by country
order by 1 desc
) as a
group by trips
Then I only get 1 entry for each country. i.e.
23 United Kingdom
10 France
4 Czech Republic
etc.
So for each row I only have 1 country listed and not all of them. i.e. Row 2 should show France & Germany and row 3 should show Czech, USA & Canada
Any ideas how to do this in mysql?
Thanks
Using GROUP_CONCAT on the country column it should yield the result you want:
SELECT trips,
GROUP_CONCAT(country) AS country
FROM (
SELECT COUNT(*) AS "trips",
country
FROM trip_view
GROUP BY country
) a
GROUP BY trips
ORDER BY 1 DESC
Output:
TRIPS COUNTRY
23 United Kingdom
10 France,Germany
4 Czech Republic,Canada,USA
Live DEMO.

How can I write a query as a value in a row?

I have two tables on MySQL (using phpMyAdmin), looking like the following:
Table 1:
Country Total Minutes
USA 100
USA 90
Canada 60
Mexico 80
UK 90
France 70
France 10
Germany 10
In Table 2, what I need to do is the following:
Region Total Minutes
North America USA+USA+Canada+Mexico Mins
Europe UK+France+France+Germany Mins
Is there a way to have a row be the result of a query?
You either need a region column in table 1:
SELECT region, SUM(`Total Minutes`)
FROM timespent
GROUP BY region;
Or a separate region <-> country table:
SELECT region, SUM(`Total Minutes`)
FROM myregions r
INNER JOIN timespent t USING (country)
GROUP BY r.region;
The regions table would look like this:
region | country
--------------+--------
North America | USA
North America | Mexico
If you can't change anything in your database, look at Andomar's solution :)
You could translate the countries to regions in a subquery. The outer query can then group by on region:
select Region
, sum(TotalMinutes) as TotalMinutes
from (
select case country
when 'USA' then 'North America'
when 'France' then 'Europe'
end as Region
, TotalMinutes
from YourTable
) as SubQueryAlias
group by
Region