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

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
)

Related

How to sum when they have same color with sql?

I have this table
i want to ignore productNo and sum all product count accordingly.
select sum(count), max(productNo)
from Table
where date between 117 and 118
group by product
this one gives wrong result...
I want to have sum of counts for each Product-ProductNo combination
try like below
select product,productno,sum(count) as result
from table_name
where productno='X1'
group by product,productno
seems you need the firts rows order by result
select product,productno,sum(count) as result
from table
group by product,productno
order by result
limit 1
Since you haven't tagged any DBMS so, i would use row_number():
select t.*
from (select product, productno, sum(count) as cnt,
row_number() over (partition by product order by sum(count) desc) as seq
from table t
group by product, productno
) t
where seq = 1;
You can also use LIMIT clause (but not for each product) :
select product, productno, sum(count) as cnt
from table t
group by product, productno
order by cnt desc
limit 1;
Some other DBMS requires TOP clause instead of LIMIT clause so, you can change accordingly but the idea would be same.
select sum(count), max(productNo)
from Table
where date between 117 and 118
group by product, productNo
with this it works :)

Select query inside a select query

I am running the following query on the table with multiple records having different quantity fields but the same id.
SELECT MIN( quantity )
FROM ( SELECT *
FROM `ready_for_delivery`
WHERE joborderid LIKE 00065
ORDER BY joborderid DESC ) a
GROUP BY quantity
It is returning all the values and not the minimum value. Any ideas why? Thanks in anticipation.
I think this is what you looking for:
SELECT MIN( quantity ) as 'Min' FROM ( SELECT * FROM `ready_for_delivery` WHERE joborderid LIKE 00065 ORDER BY joborderid DESC )a
If you are only after a single value, you should not be using a group by.
Definition for Group by:
A GROUP BY clause works on the rows returned by a query by summarizing identical rows into a single/distinct group and returns a single row with the summary for each group, by using appropriate Aggregate function in the
SELECT list, like
COUNT()
SUM()
MIN()
MAX()
AVG().

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;

How can I limit query's results without using LIMIT

I need to show ordered 20 records on my grid but I can't use LIMIT because of my generator(Scriptcase) using LIMIT to show lines per page. It's generator's bug but I need to solve it for my project. So is it possible to show 20 ordered record from my table with a query?
As from comments,if you can't use limit then you can rank your results on basis of some order and in parent select filter limit the results by rank number
select * from (
select *
,#r:=#r + 1 as row_num
from your_table_name
cross join (select #r:=0)t
order by some_column asc /* or desc*/
) t1
where row_num <= 20
Demo with rank no.
Another hackish way would be using group_concat() with order by to get the list of ids ordered on asc/desc and substring_index to pick the desired ids like you need 20 records then join with same table using find_in_set ,But this solution will be very expensive in terms of performance and group_concat limitations if you need more than 20 records
select t.*
from your_table_name t
join (
select
substring_index(group_concat(id order by some_column asc),',',20) ids_list
from your_table_name
) t1 on (find_in_set(t.id , t1.ids_list) > 0)
Demo without rank
What about SELECT in SELECT:
SELECT *
FROM (
-- there put your query
-- with LIMIT 20
) q
So outer SELECT is without LIMIT and your generator can add own.
In a Scriptcase Grid, you CAN use Limit. This is a valid SQL query that selects only the first 20 records from a table. The grid is set to show only 10 records per page, so it will show 20 results split in a total of 2 pages:
SELECT
ProductID,
ProductName
FROM
Products
LIMIT 20
Also the embraced query works out well:
SELECT
ProductID,
ProductName
FROM
(SELECT
ProductID,
ProductName
FROM Products LIMIT 20) tmp

MySQL select SUM of results with a LIMIT

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