SQL groupby rollup vs union - mysql

Sql question:
enter image description here
competitor country
Acme Corp USA
GLOBEX USA
Openmedia France
K-bam USA
Hatdrill UK
Hexgreen Germany
D-ranron France
Faxla Spain
the output should be
country competitors
France 2
Germany 1
Spain 1
UK 1
USA 3
Total: 8
except using groupby with rollup, i am trying to solve it via "union" but turns out "order by is not functioning" (supposed to order by country name, but my output turns out to "order by competitors" ...)
This is my code:
(select country, count(competitor) as competitors
from table
group by 1
order by 1
)
union all
(select "Total:" as country, count(*) as competitors from table);
Any help would be appreciated! Thank you!

If you want the result ordered, you need an order by after the union:
(select country, count(competitor) as competitors
from table
group by 1
) union all
(select 'Total:' as country, count(*) as competitors
from table
)
order by (country = 'Total:') desc, country asc

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.

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

How to select one column with all distinct values based on some clause

I essentially like to have one query which I'll execute one time and like to have the result (no multiple query execution) and definitely, the query should use simple MySQL structure (no complex/advanced structure to be used like BEGIN, loop, cursor).
Say I've two tables.
1st Table = Country (id(PK), name);
2nd Table = Businessman (id(PK), name, city, country_id(FK))
Like to SELECT all countries, whose businessmen are from distinct cities. No two businessmen exist in one country, who are from the same city. If so, that country will not be selected by the SELECT clause.
Country
id name
1 India
2 China
3 Bahrain
4 Finland
5 Germany
6 France
Businessman
id name city country_id
1 BM1 Kolkata 1
2 BM2 Delhi 1
3 BM3 Mumbai 1
4 BM4 Beijing 2
5 BM5 Paris 6
6 BM6 Beijing 2
7 BM7 Forssa 4
8 BM8 Anqing 2
9 BM9 Berlin 5
10 BM10 Riffa 3
11 BM11 Nice 6
12 BM12 Helsinki 4
13 BM13 Bremen 5
14 BM14 Wiesbaden 5
15 BM15 Angers 6
16 BM16 Sitra  3
17 BM17 Adliya 3
18 BM18 Caen 6
19 BM19 Jinjiang 2
20 BM20 Tubli 3
21 BM21 Duisburg 5
22 BM22 Helsinki 4
23 BM23 Kaarina 4
24 BM24 Bonn 5
25 BM25 Kemi 4
In this respect, China and Finland shouldn't be listed.
I've attempted using count and group by, but no luck.
Can you please help me to build up this query.
Here it is, all you need is to join Businessman table and count cities and distinct cities and if they equal that means all businessmen are from different cities:
SELECT
c.`id`,
c.`name`,
COUNT(b.`id`) AS BusinessmanCount,
COUNT(b.`city`) AS CityCount,
COUNT(DISTINCT b.`city`) AS DistinctCityCount
FROM `countries` c
INNER JOIN Businessman b ON c.`id` = b.`country_id`
GROUP BY c.`id`
HAVING CityCount = DistinctCityCount
For minified version what you exactly need:
SELECT
c.`id`,
c.`name`
FROM `countries` c
INNER JOIN Businessman b ON c.`id` = b.`country_id`
GROUP BY c.`id`
HAVING COUNT(b.`city`) = COUNT(DISTINCT b.`city`)
Well, I think we should have waited for you to show your own query, because one learns best from mistakes and their explanations. However, now that you've got answers already:
Yes, you need group by and count. I'd group by cities to see if I got duplicates. Then select countries and exclude those that have duplicate cities.
select *
from country
where id not in
(
select country_id
from businessmen
group by city, country_id
having count(*) > 1
);
You need either nested aggregations:
select *
from Country
where id in
(
select country_id
from
(
select city, country_id,
count(*) as cnt -- get the number of rows per country/city
from Businessman
group by city, country_id
) as dt
group by country_id
having max(cnt) = 1 -- return only those countries where all counts are unique
)
Or compare two counts:
select *
from Country
where id in
(
select country_id
from Businessman
group by country_id
having count(*) = count(distinct city) -- number of cities is equal to umber of rows
)

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