MySQL select SUM of results with a LIMIT - mysql

I have a table full of items and prices for said items. I would like to grab the SUM of the 3 highest priced items.
I thought perhaps SELECT SUM(items.price) FROM items ORDER BY items.price LIMIT 3 but that does not seem to do the trick. Is this possible? Thanks!

select sum(price) from (select items.price from items order by items.price desc limit 3) as subt;

LIMIT affects the number of rows returned by the query and SUM only returns one. Based on this thread you might want to try:
SELECT sum(price)
FROM (SELECT price
FROM items
ORDER BY price DESC
LIMIT 3
) AS subquery;

Just use a sub-select:
select sum(prices) from (SELECT SUM(items.price) FROM items ORDER BY items.price LIMIT 3) as prices

Use a subquery or something like that. Just an idea, as I have not tested the actual query.
SELECT SUM(items.price) from (select price FROM items ORDER BY items.price LIMIT 3)

I haven't tested this and I just wrote it on my memory. You could try something like:
SELECT * AS total FROM items ORDER BY price DESC
SELECT SUM(price) FROM total LIMIT 3;
Edit: I was slow

Related

How to find Maximum of average in group by clause in SQL

Hi I have a query like this
select avg(TotalSale) as value from Sales group by SalesPerson;
which gives me the output as
value
50.0000
250.0000
62.5000
I want to just get the max out of the values. What should i do?
I tried
select max(avg(TotalSale)) as value from Sales group by SalesPerson; which is wrong.
select avg(TotalSale) as value from Sales group by SalesPerson order by value desc limit 1; which works partially.
What would be better approach to do this?
Wrap your query....
select max(avgvalue)
from
(
select avg(totalsale) as avgvalue
from sales
group by salesperson
)z
Use order by and fetch one row:
select avg(TotalSale)
from Sales
group by SalesPerson
order by avg(TotalSale) desc
limit 1;
You can also get the SalePerson involved with this as well.
I guess it can be done in a single query .Try this :
SELECT
Max(Totalsale) as Value
FROM
Sales
ORDER BY avg(Totalsale)
LIMIT 1;

max and count query

I'm trying to make a query with max and count like this one: (taken from http://www.w3resource.com/sql/aggregate-functions/max-count.php)
SELECT MAX (mycount)
FROM (SELECT agent_code,COUNT(agent_code) mycount
FROM orders
GROUP BY agent_code);
this query returns a column with the name 'MAX(MYCOUNT)' with the max value: '7',the simple change I want is that I would like to get the agent code of the one who got the maximum, instead of the max records of agent code.
tried to do this in some ways but no luck so far,
hope you can help me to do this right.
If you don't have to worry about a tie for two agents having the max number of orders, then you can try the following:
SELECT agent_code, COUNT(*) AS mycount
FROM orders
GROUP BY agent_code
ORDER BY COUNT(*) DESC
LIMIT 1
If you do have to worry about a tie for the max number of orders, and you want all ties, then you can use a subquery:
SELECT agent_code, COUNT(*) AS mycount
FROM orders
GROUP BY agent_code
HAVING COUNT(*) = (SELECT MAX(t.mycount) FROM
(SELECT COUNT(*) AS mycount FROM orders GROUP BY agent_code) t)
You can use order by and limit:
SELECT agent_code, COUNT(agent_code) as mycount
FROM orders
GROUP BY agent_code
ORDER BY mycount DESC
LIMIT 1;

Mysql How can i find max and min after sum a column

This is my code from tables
i want to select from it the first two and the last two rows as a result. is that possible?
in other words i want to select min and max of a column after it was summed. prefer two of each if possible
thanks a lot
`select A.prod_id, SUM(quantity) Total
from charging A
group by prod_id
order by total`
You can use a subquery. Because you didn't bother to include your query in the question, I am not going to re-type it. For just the values:
select min(Total), max(Total)
from (<your query here>) s
For four rows with both values:
(select t.*
from (<your query here>) t
order by Total asc
limit 2
) union all
(select t.*
from (<your query here>) t
order by Total desc
limit 2
)

SQL statement Max(Count(*))

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.

MySQL - Select highest values from one column, then re-order based on a second column?

I think what I need to do can be done using one query, but I'm really not sure - and I'd like to avoid performing a query and then sorting the resultant array if possible.
Basically, I have one table, which includes the following columns:
product_name, price, sold
From these columns, I'd like to do the following:
Select the highest 20 values from the 'sold' column DESC;
Order the 20 results by price ASC.
Sounds so simple, but can't figure out how to accomplish this to save my life, and SQL is not my strong point. If anyone could help out, it would be appreciated!
You can use subqueries for this:
select t.*
from (select t.*
from t
order by sold desc
limit 20
) t
order by price asc
You have a query that does a bunch of stuff. I'll call this . Here is what you do:
select t.*
from (select t.*
from (<subquery
) t
order by sold desc
limit 20
) t
order by price asc
I think this will do what you are looking for:
select * from table
order by sold desc, price asc