I have a table called ranks which stores a 1-7 value and connects it with an item and user id. An item's rating is the average of all ratings it has been given.
Ranks table: id | userid | value | itemid.
To sort by average highest rating, I do (from an answer here):
select avg(value), itemid
from ranks
group by itemid
order by avg(value) desc
However, I also want to be able to filter items with a certain rating, not just highest rated -> lowest rated. For example, items with an average rating beteen 5 and 6.
select avg(value), itemid
from ranks
having avg(value) between 5 and 6
group by itemid
order by avg(value) desc
This gives me a nonspecific error at group by. Can anyone point me in the right direction?
Your having clause needs to come after your group by clause, so just reverse the order they appear in:
select avg(value), itemid
from ranks
group by itemid
having avg(value) between 5 and 6
order by avg(value) desc
select * from
(
select avg(value) as avgval, itemid
from ranks
group by itemid) t
where avgval between 5 and 6;
You can do it with a sub-query.
with Temp as
(
select avg(value) [rating], itemid
from ranks
)
Select * from Temp
Where rating between 5 and 6
group by itemid
order by rating desc
Related
I need to get the students who get the highest total score in the table while there are more than one student getting the highest score. Why the result is different in the following 2 queries?
SELECT * FROM
(
SELECT SUM(points) total, student_id
FROM student_score_per_subject_table
GROUP BY student_id
ORDER BY 1 DESC
) students
HAVING total = MAX(total)
The result of this query has only one return and the student_id the alphabetically smallest one;
SELECT * FROM
(
SELECT SUM(points) total, student_id
FROM student_score_per_subject_table
GROUP BY student_id
ORDER BY 1 DESC
) students
HAVING total = 100 -- highest total score
The result is desired with all highest-scorers being returned.
----- EDIT --------
MySQL version: 5.7.12
Write a query to display the customer name who visited the second highest number of times
select customer_id,count(*) from booking group by customer_id ;
using this query i got the count of number of visits for each customer as shown below
CUSTOMER_ID,COUNT(*)
C001,6
C002,1
C003,1
C004,1
C005,4
but i want to display only c005 since he has visited the second maximum time
SELECT customer_id, COUNT(*)
FROM booking
GROUP BY customer_id
HAVING COUNT(*) <> (SELECT MAX(t.custCount)
FROM (SELECT COUNT(*) AS custCount
FROM booking
GROUP BY customer_id) t )
ORDER BY COUNT(*) DESC
LIMIT 1
As a side note, this won't work if there are ties for second place. In this case, you use the above query as a condition in the WHERE clause, e.g.
SELECT customer_id
FROM booking
GROUP BY customer_id
HAVING COUNT(*) = (query given above)
You can use a outer query and filter the same like
select customer_id from (
select customer_id,
count(*) as datacount
from booking
group by customer_id ) xxx
order by datacount desc
limit 1;
Forewarning ,I'm a total newbie so be gentle.I need to get the SUM of qty from a set of values and then the top 5 MAX sum qty from that list. But I need to leave out a NULL value from final list.
I would do this without a subquery as:
select game_name, sum(qty) AS total
from sales
where game_name is not null
group by game_name
order by total desc
limit 5;
Try this:
SELECT game_name,
max(total) AS total
FROM ( select game_name,
sum(qty) AS total
FROM sales
GROUP BY game_name
) AS RANK
where game_name is not null
group by game_name
order by total desc limit 5 ;
It will remove any null game, order by total descending and it gives you the first 5 rows.
I have a table called order_list with the following fields
order_id
user_id
item_id
count
date
order_status
every time users place order, this table used to save order details. so how can I write my SQL query to find top 10 items based on the sum of ordered count?
SELECT item_id, SUM(count) FROM order_list GROUP BY item_id ORDER BY SUM(count) DESC LIMIT 0,10
SELECT *
FROM table_name
GROUP BY item_id
ORDER BY count DESC
LIMIT 10
select item_id, sum(count) as total
from order_list
group by item_id
order by total desc
limit 10
I have a table with feilds : file_id, rating, user_id
There is one rating per user_id, so there could be many rating (in scale of 0-5) for a single file_id.
I want to find avg of ratings for every file_id and then display 5 file_id with highest avg rating.
Actually my sql query looks like:
SELECT m.server_domain, m.original_name, m.type, m.title, m.views,
m.description, m.hash, AVG(mr.rating_scale5) as avg_rating_scale5
FROM c7_media m, c7_storage s, c7_media_ratings mr
WHERE s.public=1 AND m.storage_hash = s.hash AND m.hash = mr.media_hash
GROUP BY mr.media_hash
How should I do this?
Zeeshan
Group by a file_id and then simply order by the average. Cut off all records that fall below the top 5.
SELECT
file_id, AVG(rating) as avg_rating
FROM
table
GROUP BY
file_id
ORDER BY
avg_rating DESC
LIMIT 5
SELECT `file_id`, AVG(`rating`) as a FROM `table`
GROUP BY `file_id` ORDER BY a DESC LIMIT 5
Replace 'table' with the name of your table.