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
Related
Is there any way how to order top rows in a query in a different way than the rest? For example, if I have a list of products with name and price, I would like to get the list ordered in a way that 10 most expensive products are on top ordered by price desc, and the rest below is ordered by product name.
What comes in my mind is something like this:
SELECT id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
ORDER BY name
but this query does not execute, it prints Incorrect usage of UNION and ORDER BY, furthermore if I wrap the selects into another selects, it prints LIMIT & IN/ALL/ANY/SOME subquery. Any idea?
select * from
(
SELECT 1 a, id,name, price FROM products ORDER BY price DESC LIMIT 10
UNION ALL
SELECT 0, id,name,price FROM products
WHERE id NOT IN
(SELECT id FROM products ORDER BY price DESC LIMIT 10)
)
order by a*price desc, name
I have a row with products and I'd like to get 10 random records, but a maximum of 1 row per user_id. Right now I have this:
SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
ORDER BY RAND() DESC LIMIT 0,12
I tried doing a SELECT DISTINCT user_id, ... but that doesn't work. The table has 100's of products and each user_id may have multiple ones, but I'd like to retrieve a maximum of 1 per user_id, but still a total of 10.
Is that possible at all without a more complex structure?
I may be missing something, but have you tried doing a GROUP BY?
SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
GROUP BY user_id -- SPECIFY FIELD HERE
ORDER BY RAND() DESC
LIMIT 0,12
This will group one row per user, or whichever field you desire to group by.
Try something like that with grouping in main query after random ordering in subquery:
SELECT * FROM
(SELECT user_id, product_id, price, name, category, is_featured
FROM db_products
WHERE category!=15 AND active=1 AND deleted=0 AND is_featured=1
ORDER BY RAND()) AS subquery
GROUP BY user_id
LIMIT 0,10
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
If you have a column called userID, what query would you use to find the userID with the greatest number of rows in that table?
Thank you
You can use COUNT, ORDER BY that count DESC and LIMIT the result to the top one:
SELECT user_id, COUNT(*)
FROM tableName
GROUP BY user_id
ORDER BY 2 DESC
LIMIT 1;
select user_id, sum(1) as counter
from TABLE
group by user_id
order by 2 desc
limit 1
This should be enough to get only one user, even if more than one user share the maximum amount of rows:
SELECT user_id FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
If you need to return all matching users:
SELECT user_id FROM table
GROUP BY user_id
HAVING COUNT(*) = (
SELECT COUNT(*) FROM table
GROUP BY user_id
ORDER BY COUNT(*) DESC
LIMIT 1
)
Basically I have a review table for product. The attributes are reviewID, reviewCustName, reviewText, productID. So I wonder is there any ways to count the product with most reviews? Here is my SQL statement:
SELECT productID, count(*) AS mostReviews, MAX(mostReviews) FROM sm_review GROUP BY productID;
I wonder is it possible to write such SQL statement? Or i there any better way?
Thanks in advance.
You can use the following to get the result. This gets the total count for each product but when you order the count in a descending order and apply LIMIT 1 it returns only the product with the most reviews:
select count(*) total
from sm_review
group by productId
order by total desc
limit 1
It should just be;
SELECT count(*) AS num_reviews FROM sm_review
GROUP BY productID ORDER BY num_reviews DESC LIMIT 1;
Note the ORDER BY num_reviews and the LIMIT 1 which limits the number of results.