I have a list of names with country and city.
| COUNTRY | CITY | NAME | ID|
I need to find a count of each name grouped by country and city. I also need percentage of each name WITHIN each city/country. For example:
RESULTS
SELECT count(ID), country, city, name
from table NAME
GROUP BY country, city, name
How do I calculate percentage of each name WITHIN each city/country?
It looks like your query doesn't compute the count of each name grouped by city and state but rather the count of each id grouped by name, city and country. Assuming this is what you wanted, then the percents might be calculated like this:
SELECT count(N.ID) as NameCount
, count(N.ID) * 100.0 / (SELECT COUNT(*) FROM NAME WHERE Country = N.Country AND City = N.City) as NamePercent
, N.Name
, N.Country
, N.City
FROM NAME N
GROUP BY N.country, N.city, N.Name
Here's a test fiddle I've created for it: http://sqlfiddle.com/#!2/875026/1/0
For the second question - from the comments,
How to calculate AVE of name occurance across different cities grouped by a country. For example: count of Mikes in NY-2, Chicago-4, LA-5. So ave for Mike in US is 3.6
You might do something like this:
SELECT Name
, Country
, AVG(NameCount) NameAvgAcrossCountry
FROM
(SELECT count(N.ID) as NameCount
, count(N.ID) * 100.0 / (SELECT COUNT(*) FROM NAME WHERE Country = N.Country AND City = N.City) as NamePercent
, N.Name
, N.Country
, N.City
FROM NAME N
GROUP BY N.country, N.city, N.Name) NamesQuery
GROUP BY Name, Country
Related
I have a table with the following values: Name, Street,I'd , Value, Date.
I need to combine Name, Street, Id and make 2 subgroups by date. I want to compare the value in row with the same name, street and id but different date. And write only the ones with different value
Example:
Mike, Street 1 , idtag , 5 , 11.5.2022
Mike, street 1 , idtag , 10 , 10.5.2022
I want to write the difference in value with the name, street, id combination.
All the solutions I have tried take way to long
dYou could use an aggregation approach here. Assuming that you want to flag any name, street, and ID combination which have 2 or more records on different dates, you may try:
SELECT name, street, ID, MAX(val) - MIN(val) AS diff
FROM yourTable
GROUP BY name, street, ID
HAVING MIN(date) <> MAX(date);
To use this logic for a specific pair of records, whose (unique) date values are known, use this version:
SELECT name, street, ID, MAX(val) - MIN(val) AS diff
FROM yourTable
GROUP BY name, street, ID
HAVING MIN(date) = '2022-05-10' AND MAX(date) = '2022-05-11';
For eg. if we go to w3schools:
And put
SELECT City, count(City) as Occurrences
FROM Customers
GROUP by City
ORDER BY count(City) DESC
But what I really want is to exclude max and min occurances (ignore hard-coded 6 and 1 values for max and min), like
SELECT City, count(City) as Occurances
FROM Customers
GROUP by City
HAVING count(City) != 6 AND count(City) != 1
ORDER BY count(City) DESC
What would be the way to get desired output without hard-coding 6 and 1?
you can try with this
select c1.city, c1.cnt from (
select city, count(*) cnt from customers c
group by city
) c1 inner join
(select max(cnt) max_cnt, min(cnt) min_Cnt from (
select city, count(*) cnt from customers c
group by city
)) c2
on c1.cnt!=c2.max_cnt and c1.cnt!=c2.min_cnt
;
as MySQL doesn't have an OVER..PARTITION BY function, which could maybe be useful.
Another approach could be on rownums and ordering but I prefer this
Try this
SELECT City, count(City) as Occurences FROM Customers,
(SELECT MAX(Occur) AS Ma,MIN(Occur) AS Mi FROM (SELECT City, count(City) as Occur
FROM Customers GROUP by City)) as T
GROUP BY City HAVING Occurences!=T.Ma AND Occurences!=T.Mi ORDER BY Occurences DESC
Given the following table:
id, country, name, age
1, Italy, Burke, 21
2, Italy, Yefrem, 20
3, Spain, Valter, 30
4, Spain, Max, 11
How can i get one oldest citizen for each country
For example result should contain only rows 1 and 3
Result should be grouped by country and in each group entry with highest age should be returned
Use an inline view to filter the rows and then a join to the table itself to get all the columns back like so:
select t.id, t.country, t.name, t.age
from test t
join (
select max(age) as age, country
from test
group by country
) s
on t.age = s.age and t.country = s.country;
Tested here: http://sqlfiddle.com/#!2/2523ce/3
i've been using group concat and concat separately in order to concatenate rows or column. However, i want to concatenate now both column data as well as the row such as:
From these given table
id disease domain
-- ------- --------
1 Typhoid Davao City
2 Pox Davao City
3 Dengue Manila City
I want the sql to output this:
disease domain
------- --------------
Typhoid: 1, Pox: 1 Davao City
Dengue: 1 Manila City
The task was concatenate or group all the diseases reported in a city, then count how many reports for each disease for each city. I know it sounds useless, but it's only given to me as an assignment, and I'm lost.
SELECT GROUP_CONCAT(CONCAT_WS(' ',disease,total))
, domain
FROM
( SELECT disease
, domain
, COUNT(*) total
FROM my_table
GROUP
BY domain
, disease
) x
GROUP
BY domain;
I have a table which stores IDs and the city where the store is located.
I want to list all the stores starting with the stores that are in the city where there are the most stores.
TABLE
ID CITY
1 NYC
2 BOS
3 BOS
4 NYC
5 NYC
The output I want is the following since I have the most stores in NYC, I want all the NYC location to be listed first.
1 NYC
4 NYC
5 NYC
2 BOS
3 BOS
SELECT count(City), City
FROM table
GROUP BY City
ORDER BY count(City);
OR
SELECT count(City) as count, City
FROM table
GROUP BY City
ORDER BY count;
Ahh, sorry, I was misinterpreting your question. I believe Peter Langs answer was the correct one.
This one calculates the count in a separate query, joins it and orders by that count (SQL-Fiddle):
SELECT c.id, c.city
FROM cities c
JOIN ( SELECT city, COUNT(*) AS cnt
FROM cities
GROUP BY city
) c2 ON ( c2.city = c.city )
ORDER BY c2.cnt DESC;
This solution is not a very optimal one so if your table is very large it will take some time to execute but it does what you are asking.
select c.city, c.id,
(select count(*) as cnt from city c2
where c2.city = c.city) as order_col
from city c
order by order_col desc
That is, for each city that you come across you are counting the number of times that that city occurs in the database.
Disclaimer: This gives what you are asking for but I would not recommend it for production environments where the number of rows will grow too large.
SELECT `FirstAddressLine4`, count(*) AS `Count`
FROM `leads`
WHERE `Status`='Yes'
AND `broker_id`='0'
GROUPBY `FirstAddressLine4`
ORDERBY `Count` DESC
LIMIT 0, 8