I have a query where I want to get the 20 most popular locations from a table, and then order them alphabetically. I'm wondering if there's a cleaner or more efficient way to do this?
SELECT
city
FROM (
SELECT
city,
count(*) AS cnt
FROM locations
GROUP BY city
ORDER BY cnt DESC
LIMIT 20
) s ORDER BY city;
Slightly cleaner:
SELECT city FROM (
SELECT city FROM locations
GROUP BY city
ORDER BY count(*) DESC
LIMIT 20
) s ORDER BY city
You don't need to retrieve the count(*) if you're not going to use it.
Related
I have a event_table as below.
city country event date
london UK Soccer 8/20/2010
toronto CA Basketball 8/12/2011
newyork US Basketball 8/21/2012
LA US Basketball 8/30/2013
.....
I need to get all fields with these conditions:
30 entries only
event = "Baseketball"
order by date
Distinct Country "US".
I tried below:
select distinct on country *
from event_table
where event="Basketball"
order by date Dec limit 20;
and below:
select *
from event_table
where event="Basketball"
order by date Dec limit 20
group by country;
But none working. Thanks for the help in advance.
Fro only one country it is enough to make
SELECT *
FROM event_table
WHERE event="Basketball"
AND country = 'US'
ORDER BY `date` DESC
LIMIT 30
If you want more counties use
SELECT
city, country, event, `date`
FROM
(SELECT *
,ROW_NUMBER() OVER(PARTITION BY county ORDER BY `date` DESC) rn
FROM event_table
WHERE event="Basketball") t1
WHERE rn <= 30
You can then select the countrie you like
I need to retrieve data from a view. View will have details such as country, location_id, content_id, content_url, content_likes and .... I need to retrieve location_id which has max(content_likes) grouped by country order by sum(content_likes) desc.
Right now I am getting the correct data based on country side, but Id I am getting which is of default order. But instead of default ordered id I need to get Id which has maximum likes.
My current query is
select * from <view_name> group by country order by sum(content_likes) desc;
Data From View:
Result :
You seem to want something like:
select country_id, location_id, sum(content_likes)
from view_name vn
group by country_id, location_id
having location_id = (select vn2.location_id
from view_name vn
where vn2.country_id = vn.country_id
group by location_id
order by sum(content_likes) desc
limit 1
);
You can try this (sorry any syntax error):
select cc.*
from
(select aa.location_id, aa.country /*[, add fields if you need them...]*/,
rank() over (partition by aa.country_id order by aa.content_likes) ranking_field
from <view_name> aa
join (select country, sum(content_likes) sum_content_likes
from <view_name>
group by country) bb
on aa.country=bb.country) cc
where ranking_field=1
order by cc.sum_content_likes desc
This returns only the location with max likes in each country ordered by the total likes in each country
EDIT:
With your new example, perhaps you can do simply this:
SELECT *
FROM <<view>> aa
JOIN(SELECT country, MAX(content_likes) max_likes
FROM <<view>>
GROUP BY country) bb
ON aa.country=bb.country
AND aa.content_likes=bb.max_likes
It is two pass query but give your example result. It can return more than one row for country if more than one location has same likes number and those are the max likes in that country.
Hope this help you.
My issue resolved with below sql
select t.location_id,t.content_likes from
(select country,max(content_likes) as mcl,sum(content_likes) as scl from test_eresh
group by country) x,
test_eresh t
where t.country = x.country
and t.content_likes = x.mcl
order by x.scl desc
I'm wondering how one would sum the results from a query?
I want to know how many people live in total in the three biggest cities in Norway. I'm using mysql, the world.sql sample database in mysql workbench.
This is the closest I've gotten
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
There's a few problems here namely this gives me three results instead of one, and while using LIMIT which actually limits how many results it gives, not how many it uses.
Any ideas?
You would use a subquery:
SELECT SUM(population)
FROM (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3
) cp
simply sum the result:
select sum(population) from (SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) t1
select sum(population) from (SELECT population FROM city WHERE
CountryCode = 'NOR' ORDER BY population DESC LIMIT 3) temp
Read on subqueries.
Make your current query a subquery and get sum from your subquery.
SELECT SUM(population) FROM (
SELECT population
FROM city
WHERE CountryCode = 'NOR'
ORDER BY population DESC
LIMIT 3) p
You query will now act as a virtual table, from which you can you can write a select query to get the sum
Is there any other way to write this query ?
I tried doing it in a subquery but it doesn't work because of the multiple columns. It seems like this query only works by itself. Please correct me
Records
PK recordId
dateViewed
CarViewed
I tried this
SELECT R.`dateViewed` FROM Records ,(
SELECT R.CarViewed, COUNT(R.CarViewed) as cnt FROM Records R
GROUP BY R.CarViewed
ORDER BY cnt DESC
LIMIT 1 ) AS favouriteCarOfTheDay
GROUP BY R.`dateViewed
Then I tried this
SELECT R.`dateViewed` ,COUNT(R.CarViewed) as cnt FROM Records ,
(
SELECT R.CarViewed FROM Records R
GROUP BY R.CarViewed
ORDER BY cnt DESC
LIMIT 1 ) AS favouriteCarOfTheDay
GROUP BY R.`dateViewed
Along many other queries I tried, I have no idea how to get it working.
In a nutshell for a specific date, I would like to get the most common cars that were viewed.
Like :
dateViewed favouriteCarOfTheDay
2012-09-22 | Nissan
2012-09-23 | BMW
try this
SELECT R.`dateViewed` ,COUNT(R.CarViewed) as cnt ,R.CarViewed FROM Records R
GROUP BY R.`dateViewed
ORDER BY COUNT(R.CarViewed) DESC
I think the following should work (disclaimer, not all my own work, adapted from an answer at another question)
SELECT DISTINCT
R.dateViewed,
R.CarViewed
FROM Records R
WHERE
R.dateViewed =
(SELECT R2.dateViewed FROM
(
SELECT R1.dateViewed, COUNT(*) AS numViewed
FROM Records R1
WHERE R1.CarViewed = R.CarViewed
GROUP BY R1.dateViewed
ORDER BY R1.numViewed DESC
) AS R2
LIMIT 1
)
ORDER BY r.dateViewed
Such things are really awful to do in MySQL so it might actually by slower than two correlated subquery but at least it returns both the car and it's viewcount:
SELECT counts.`dateViewed`,counts.`CarViewed` as favourite_car, counts.cnt
FROM
(SELECT R.`dateViewed` ,R.`CarViewed`, COUNT(*) as cnt
FROM Records
GROUP BY R.`dateViewed` ,R.`CarViewed`
) as counts JOIN
(SELECT R.`dateViewed`, MAX(cnt) as cnt
FROM
(SELECT R.`dateViewed` ,R.`CarViewed`, COUNT(*) as cnt
FROM Records
GROUP BY R.`dateViewed` ,R.`CarViewed`
) as q
GROUP BY R.`dateViewed`) as maxes
ON counts.cnt=maxes.cnt
Table structure:
country
season
points
Current query:
SELECT SUM(points) AS total, country
FROM table
WHERE season >= X
GROUP BY country
ORDER BY total desc
This gives me a nice list ordered by a total points collected by a given country. BUT, if a country is tied with another country, I want to sort by their points in the latest season given, is that possible within the same query? And if so, how? (Remember its grouped at the moment)
example of rows
denmark (country), 1 (season), 10 (points)
denmark (country), 2 (season), 5 (points)
sweden(country), 1 (season), 5 (points)
sweden (country), 2 (season), 10 (points)
Maybe this'll do your trick:
SELECT SUM(points) AS total, country
FROM table
WHERE season >= X
GROUP BY country
ORDER BY total DESC, (SELECT t2.points FROM table t2 WHERE table.country=t2.country ORDER BY t2.season LIMIT 1) DESC
SELECT grp.total, grp.country
FROM
( SELECT SUM(points) AS total, country, MAX(season) AS max_season
FROM table
WHERE season >= X
GROUP BY country
) AS grp
LEFT JOIN
table AS t
ON t.country = grp.country
AND t.season = LATEST_SEASON
ORDER BY
grp.total DESC
, t.points DESC ;