I have the following data set.
How would the query look if wanted the most occuring show on top and limit the result for 20 shows?
EDIT
I have searched on the web for this and i need to use the GROUP BY method from sql. but when i make the query
SELECT `show` FROM fans GROUP BY `show` LIMIT 20
i do not get the desired result.
SELECT a.*, b.TotalCount
FROM TableName a
INNER JOIN
(
SELECT c.show, COUNT(*) totalCount
FROM TableName c
GROUP BY c.show
) b ON a.show = b.show
ORDER BY b.TotalCount DESC
LIMIT 20
if you want to list one record for every show, you can simply use GROUP BY
SELECT a.show, COUNT(*) TotalCount
FROM TableName a
GROUP BY a.Show
ORDER BY TotalCount DESC
LIMIT 20
Related
Actually I'm working with the following table fsa_areas:
Note that each area has a responsible
Now, what I need to do, is to order the same table as following:
Note that now the results are ordered by the the responsible with more areas and at the end the responsible with less areas.
Is there a way to order them in that way?
You can use a COUNT subquery in the ORDER BY clause:
select a.*
from fsa_areas a
order by (select count(*) from fsa_areas a1 where a1.Responsible = a.Responsible) desc
Another way is to get the count in a derived table and join the base table to it
select a.*
from (
select Responsible, count(*) as cnt
from fsa_areas
group by Responsible
) r
join fsa_areas a using(Responsible)
order by r.cnt desc
In MySQL 8 you can use COUNT() as window function:
select *, count(*) over (partition by Responsible) as cnt
from fsa_areas
order by cnt desc
Question title pretty much sums it up.
I'm joining two tables with the results ordered by a timestamp column, & sorted in ascending order (so the most recent rows are at the bottom). How do I get the last 20 rows in this case?
Code (Assume the two tables are Table A & Table B):
SELECT A.*, B.some_column
FROM A
INNER JOIN B ON B.id = A.id AND (other conditions here)
ORDER BY A.timestamp ASC
LIMIT 20
This currently returns the first 20 rows. I need the last 20.
P.S. I need the final result set to be sorted in ASCENDING order. The most recent rows need to be at the end, not the beginning!
SELECT * FROM (
SELECT A.*, B.some_column
FROM A
INNER JOIN B ON B.id = A.id AND (other conditions here)
ORDER BY A.timestamp DESC
LIMIT 20
) AS RESULT
ORDER BY RESULT.timestamp ASC
Try like this
SELECT * FROM
(
SELECT A.*, B.some_column
FROM A
INNER JOIN B ON B.id = A.id AND (other conditions here)
ORDER BY A.timestamp Desc
LIMIT 20
) Temp
ORDER BY A.timestamp ASC
If you want to order it by ASC in the query you could:
SELECT A.*, B.some_column
FROM A
INNER JOIN B ON B.id = A.id
WHERE id IN (SELECT * FROM (
SELECT id FROM A ORDER BY timestamp DESC LIMIT 20
) AS s) ORDER BY A.timestamp ASC
Invert the order of the result, and
In SQL server, use:
select top 20 ...
In MySQL, use:
select ... order by num desc limit 20
First, sort the other way:
ORDER BY `timestamp` DESC
LIMIT 0,20
I'm trying to select the SUM of a few values with a limit of 2. I've seen a couple of example of this (9877872) but have been unable to get it working. I have
SELECT SUM(points)
FROM
(SELECT points FROM user_table
WHERE city=1 AND group =5)
AS min_points ORDER BY points DESC LIMIT 2
However the limit clause at the end of the query does not seem to be executed and I just get the sum of all the users in the table...
Would anyone know what I'm doing wrong...?
Try this
SELECT SUM(points)
FROM
(SELECT points FROM user_table
WHERE city=1 AND group =5 ORDER BY points DESC LIMIT 2) AS t1
I want to make some mysql query, query out 5 image order by date, but from 5 different upload user.
mysql datatable Structure: id, user_id, image, text, date
SELECT *
FROM mytable
WHERE image!=''
GROUP BY user_id
ORDER BY date DESC
LIMIT 5
But this GROUP BY always cause get the user_id earlier upload image then make a ORDER BY date DESC, I need the fresh 5 images which uploaded by 5 differents users. Maybe should use some UNION, ask for a help, thanks.
Use INNER JOIN with MAX
SELECT
mytable.*
FROM
mytable
INNER JOIN
(SELECT MAX(id) AS ID, id FROM mytable GROUP BY user_id) AS m
ON m.id = mytable.id
WHERE image != ''
GROUP BY user_id
ORDER BY DATE DESC
LIMIT 5
This will fetch the max(latest) id and integrate it to the outer select
Try this query
SELECT *
FROM temp
WHERE image!=''
GROUP BY user_id
HAVING date=max(date)
LIMIT 5
Not sure about mysql but this works in sybase.
Is it possible to pull 2 results from a sub query in a sql statement?
I have:
"SELECT
(SELECT bid FROM auction_bids WHERE itemID=a.id ORDER BY bid DESC LIMIT 1) as topbid,
a.* FROM auction_items a ORDER BY a.date DESC LIMIT 15"
The part where it returns the topbid, i'd also like it to pull not only bid (as topbid) but also date (as topdate) as well. How can I do that? Do I need another sub query or can it pull both in one?
Dependent subquery (depending on some values outside, like a.id in your case) is not a very efficient way to find maximum values in subsets.
Instead use a subquery with GROUP BY:
SELECT b.topbid, b.topdate, a.*
FROM auction_items a
LEFT JOIN
( SELECT itemID, MAX(bid) as topbid, MAX(date) as topdate
FROM auction_bids
GROUP BY itemID ) b
ON a.id = b.itemID
ORDER BY a.date DESC
LIMIT 15