I need to retrieve data from a countries table to use for formatting search results but i don't seem to have the correct sql format.
Each country is stored with country_code, country_name, iso_code and sales_location
So for instance it would have 222, United Kingdom, GB, UK Sales
What i need is to be able to query how many times a sales location is listed and what country names are associated with that sales location (Rest of the World has 221 country names)
I tried
SELECT sales_location, countries_name, COUNT( sales_location )
FROM countries
GROUP BY sales_location
which does give me the locations and counts, but the country name is only giving the first country alphabetically.
SELECT sales_location, GROUP_CONCAT(countries_name), COUNT( * )
FROM countries
GROUP BY sales_location
SELECT countries_name, GROUP_CONCAT(sales_location) as sales_location,
COUNT(sales_location ) as location_count
FROM countries
GROUP BY countries_name
SELECT sales_location, GROUP_CONCAT(countries_name), COUNT(sales_location)
FROM countries
GROUP BY sales_location
This query should help.
Related
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;
I got a problem with gettin above average of numberofcities per country. Let me explain my problem, i have a table named City where has a column name Country, from there i get count the number of cities per country, after that i calculate the average of cities in total, where i get the approximately 13 cities per country. And finally i need to calculate or to get countries which have 10% more cities than average?
This is my query :
select avg(country_city_counts.numberofcities) as average -- getting average
from
(
select Country, count(*) as numberofcities -- getting number of cities per country
from city
group by Country
order by Country
) country_city_counts
You can use window functions:
select c.*
from (select Country, count(*) as numberofcities,
avg(count(*)) over () as avg_numberofcities
from city
group by Country
) c
where numberofcities > numberofcities * 1.1
order by Country
A question for homework is to show the total amount of houses with multiple presents. The list below shows which ones they are but I cannot work out the query to show them as a total of 6. I am still new and learning Mysql, my apologies for the ignorance.
Mysql data
**address** **Number of presents per home**
2 Bay Road 2
2a Neptune Road 2
45 Bay Road 2
59 Black Street 2
65 Mainway Avenue 3
89 White Street 2
Query used:
SELECT address, SUM(goodBehaviour) AS `Number of Houses with Multiple presents`
FROM wishlist
GROUP BY address
HAVING SUM(goodBehaviour) >1;
I have tried a few other queries to total the Address column but have not been able to show my desired output. Thanks.
The problem is that you sum the goodBehaviour field's values, but you should count the number of addresses that have more than 1 presents.
If each address has just 1 record in your table (based on your sample data):
select count(address)
from wishlist
where goodBehaviour >1
If you can have multiple records for a single address, then in a subquery you need to sum the number of presents and count the number of addresses in the outer query, where the total number of presents are more than 1:
select count(address)
from
(select address, sum(goodBehaviour) as presents
from wishlist
group by address) t
where t.presents>1
If you need total number of houses - you can use your query as subquery:
SELECT count(*) FROM (SELECT address, SUM(goodBehaviour) AS `Number of Houses with Multiple presents`
FROM wishlist
GROUP BY address
HAVING SUM(goodBehaviour) >1) x;
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)
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