MySQL query for grouping data - mysql

can anyone please guide me with writing MySQL query for following scenario.
The data in table is like this,
Table Name: user
user_id country city age
----------------------------------------------
1 India Mumbai 22
2 India Mumbai 22
3 India Delhi 22
4 India Delhi 23
5 India Chennai 23
6 China Beijing 20
7 China Beijing 20
8 China Shanghai 20
9 USA New York 30
10 USA New York 30
11 USA New York 30
12 USA Los Angeles 31
13 USA Los Angeles 31
14 USA Los Angeles 40
I want result to be like this which is basically sum of all users in particular country's city having same age.
country city age age_count
----------------------------------------------
India Mumbai 22 2
India Delhi 22 1
India Delhi 23 1
India Chennai 23 1
China Beijing 20 2
China Shanghai 20 1
USA New York 30 3
USA Los Angeles 31 2
USA Los Angeles 40 1

Try this :;
SELECT country,
city,
age,
count(user_id) AS age_count
FROM user
GROUP BY country,
city,
round(age)

select country, city, age, count(*) age_count
from user
group by country, city, age

Related

MySQL query to count occurrences on days of the year

I have a (large) table like this:
id venue city date time
1 Waldorf Hotel London 2020-01-01 07:00
2 Waldorf Hotel London 2020-01-02 07:00
3 Heathrow London 2020-01-02 14:00
4 Lennon Airport Liverpool 2020-01-02 16:00
5 Port of Liverpool Liverpool 2020-01-02 19:30
6 Port of Liverpool Liverpool 2020-01-03 07:00
7 Port of Liverpool Liverpool 2020-01-04 07:00
8 Port of Liverpool Liverpool 2020-01-05 07:00
9 Port of Liverpool Liverpool 2020-01-06 07:00
10 Manchester Airport Manchester 2020-01-06 12:40
11 Heathrow London 2020-01-06 14:40
So this person has been in Liverpool on 5 different days, in London on 3 different days, and in Manchester on 1 day only.
I am looking for a query that gets me this number of days that a certain city has been visited, resulting in this list:
Liverpool 5
London 3
Manchester 1
But now I'm stuck at this query that counts the occurrences per day and I can't get it right:
SELECT city, COUNT(city) AS value FROM visits WHERE YEAR(dt) = 2020 GROUP BY city, DATE(dt)
One way to achieve this is to select all the distinct city/date combinations in a subquery and then count the occurrence of each city:
SELECT city, COUNT(city) AS value
FROM (
SELECT DISTINCT city, DATE(dt)
FROM visits
WHERE YEAR(dt) = 2020
) v
GROUP BY city
Output:
city value
Liverpool 5
London 3
Manchester 1
Demo on SQLFiddle

SQL - Trying to correlate data

I'm just learning SQL. The data set I have looks like this:
city_name | work_place_name | min_commute_time | max_commute_time
-------------------------------------------------------------------
Austin CONGRESS 25 45
Austin NASA 10 12
Austin CIRCUS 16 35
CEDAR PARK CONGRESS 35 65
CEDAR PARK NASA 28 60
CEDAR PARK CIRCUS 26 55
KYLE CONGRESS 50 85
KYLE NASA 60 100
KYLE CIRCUS 60 100
I'm trying to figure out which city will have a min commute time of less than or equal to 30 for both CONGRESS and NASA. I came up with the following query, but I'm not getting the results that I am looking for.
SELECT city_name
FROM commute_times
WHERE min_commute_time<=30 AND (work_place_name='NASA' OR work_place_name='CONGRESS')
The results that I am getting are:
city_name
-----------
Austin
Austin
Cedar Park
The results that I am hoping for are:
City_name
-----------
Austin
You are close. Aggregation will fill in the missing piece:
SELECT city_name
FROM commute_times
WHERE min_commute_time <= 30 AND
work_place_name IN ('NASA', 'CONGRESS')
GROUP BY city_name
HAVING COUNT(*) = 2;

How to count number of occurrence in a filed table

if I have a table with a field City like this:
City
------
London
Los Angeles
London
Athens
Rome
Paris
Paris
How could I get this result?
City | CityNumb
-------------------
London 2
Athens 1
Los Angeles 1
Paris 2
Rome 1
select City, count(*) as CityNumb
from your_table
group by City
Above image shows the SQL query and results for above task

counting occurrences in an SSRS table

Working on SSRS. Have the following data set.
ID City Country
1 London England
2 Glasgow Scotland
3 Aberdeen Scotland
4 Swansea Wales
5 London England
6 Glasgow Scotland
7 Glasgow Scotland
8 Manchester England
Would like to have a table, grouped by the city and showing how many occurrences there have been for each city, see below?
City Country Total
Glasgow Scotland 3
London England 2
Manchester England 1
Swansea Wales 1
Aberdeen Scotland 1
How would i go about doing this please?
After creating a group of City use the following expression:
=COUNT(Fields!City.Value,"City")
Let me know if this helps.
It seems from your question that you don't want the detail in the report, only the summary data. If you do want detail, Alejandro Zuleta's answer will work. If you don't need the detail, you can do the grouping in your dataset:
select city, country, count(*) as total
from <yourtable>
group by city, country;

MySQL - assort and count items

My table has three columns 'name', 'city' and 'country'.
Now I want to make a list with only those countries, which has at least 3 times the SAME city.
name city country
---- ---- -------
Smith Boston USA
Wayne St. Louis USA
Miller Houston USA
Joseph Houston USA
Obama Washington USA
Jones Houston USA
Sarkozy Paris France
Merle Paris France
Gabin Marseille France
Delon Avignon France
Deneuve Avignon France
Trappatoni Rome Italy
Linguini Milano Italy
Mastroianni Rome Italy
Meier Hamburg Germany
Müller Munich Germany
Schmidt Hamburg Germany
Böttcher Hamburg Germany
Kunau Hannover Germany
Wilhelm Munich Germany
-------------------------------
USA
Result:
Germany
I tried it with distinct, count, group by etc. But without results.
Group by country and city then take only those having at least 3 entries each.
select distinct country
from your_table
group by country, city
having count(*) >= 3