Row with maximum values in mysql - mysql

I have table which looks like this
sales units
100 1
200 3
100 2
200 4
100 5
100 1000
I want to select maximum sales with maximum units
for above example output should be
100 1000
200 4
i tried to use max function it gives wrong answer
e.g 200 1000

select * from youtTable where sales =(select max(sales) from yourTable);

select sales, units
from tab
order by sales desc, units desc
limit 1
Since you have changed your question, the answer is:
select sales, max(units) as units
from tab
group by sales

Order by sales, then units, and take the first record:
select sales, units
from TheTable
order by sales desc, units desc
limit 1
Result:
sales units
------ ------
200 4
Edit:
For the new output that you want, you need to group on the sales value, and use the max aggregate to get the highest units value in each group:
select sales, max(units)
from TheTable
group by sales
order by sales
Result:
sales units
------ ------
100 1000
200 4

Related

How do I find the average of the result of a group by SQL query?

So assuming I'm starting with a table called Inventory like the following where there are multiple unique items per non unique storage ID:
ItemID
StorageID
453
100
234
100
642
150
234
200
343
200
143
200
I group the items based on its storage ID so it would result in the following table - (select itemID, storageID from Inventory group by storageID)
ItemID
StorageID
453, 234
100
642
150
234, 343, 143
200
But then here is the part I'm stuck on: I want to return a single numerical result representing the average number of items per storage. So that would involve counting the number of items per each distinct storage (ie: storageID 100 has 2 items, storageID 200 has 3 items) and then finding an average. So in the example I shared, the average would be (2+1+3)/3 = 2.67 items/storage.
How could I query MySQL for this? Do I even need to use group by as a start?
Group your data and use count() on each group.
select avg(cnt)
from
(
select count(*) as cnt, storageID
from Inventory
group by storageID
) tmp
You can use group by as follows:
select avg(cnt) as average_
(select count(*) as cnt
from your_table t
group by storageid) t
You don't need a subquery:
select count(*) / count(distinct storageID) as avg_per_storageID
from Inventory ;
The average is the total number of items divided by the number of storageIDs.

Finding average highest salary

Write a query to find the highest average sales among all the salespersons using the given table.
Table: Sales
Field Type
InvoiceNo Integer
SalesPerson Text
TotalSale Integer
Sample
InvoiceNo SalesPerson TotalSale
1 Acheson 50
2 Bryant 25
3 Bennett 250
4 Acheson 50
5 Bryant 100
6 Bennett 250
Sample output
max(totalsale)
250.0000
I tried using: SELECT MAX(TotalSale) FROM Sales. My output for sample test case was 250 but it was given wrong answer verdict in an online judge.
Also, when I changed the query to : SELECT MAX(TotalSale*1.0000) FROM Sales,I got correct answer for sample test case but still wrong answer for other test cases.
What is meant by average here? What should be the query?
Also, this isn't from any on-going competition. It is from a practice test which you can attempt here: https://www.hackerearth.com/challenge/test/kredx-data-analyst-test/
1.First you have to calculate the average TotalSale for each SalesPerson using the AVG function.
SELECT SalesPerson, AVG(TotalSale) AS 'TotalSale'
FROM Sales
GROUP BY SalesPerson
2.Find out the max value from the table generated by the above query.
SELECT MAX(avgSales.TotalSale)
FROM (SELECT AVG(TotalSale) AS 'TotalSale' FROM Sales GROUP BY SalesPerson) AS avgSales
SELECT avg(TotalSale) as MaxTotal
FROM Sales
GROUP BY Salesperson
ORDER BY MaxTotal desc
LIMIT 1
Output:
MaxtTotal
250.0000
The average sale would use the avg() function:
select salesperson, avg(totalsale) as avg_totalsale
from sales
group by salesperson;
The maximum of the average can be obtained using order by and limit:
select avg(totalsale) as avg_totalsale
from sales
group by salesperson
order by avg_totalsale desc
limit 1;
Here is another way to answer this:
select avg(TotalSale)
from Sales
group by SalesPerson
order by totalSale desc limit 1;
output:
avg(TotalSale)
250.0000
Minor change to #gordon-linoff 's answer to pass the sample test case.
select CAST(avg(totalsale) AS DECIMAL(16,4)) as avg_totalsale
from sales
group by salesperson
order by avg_totalsale desc
limit 1;
CAST(avg(totalsale) AS DECIMAL(16,4))
The above part will restrict the zero's in decimal place up to 4. Convert 250.00000000 to 250.0000
SELECT max(avgsale.TotalSale)
FROM (Select Cast(Round(Avg(TotalSale),2) as dec(10,4)) 'TotalSale' from Sales
GROUP BY salesPerson) avgsale

How to write a query to display SUM of the quantity if same product having same price in my inventory?

My table looks like
Product Price Qty
A 100 10
B 200 30
A 100 15
A 150 20
Two times Price 100 is repeating for product A in my table. It should add the quantity if price value same for the product. My result should be like as follows
Product Price Qty
A 100 25
A 150 20
B 200 30
Just use group by product, price with sum aggregation as:
select product, price, sum(qty)
from tab
group by product, price
order by product, price;
use sum() function with group by product and price
select t.product, t.price,sum(t.qty) as Qty from your_table t
group by t.product,t.price

Group result set by weeks

I have a schema:
item | sales
1 10
1 20
1 30
2 10
2 20
2 30
How can I group the result so it's like:
item | total
1 60
2 60
I've tried
select sum(sales) as total, week from shop
where item = 1
order by total DESC
I'm not sure how to make it work without a lot of OR clauses
You have to group by item in order to sum per item and not the complete data in the table
select sum(sales) as total, item
from shop
group by item
order by total DESC
You should use a group by statement :
select item, sum(sales) as total
from shop
group by item
order by total desc
try this:
select item, sum(sales) as total from shop
GROUP BY item
ORDER BY total DESC

How to form sql structure to get max total value

I have a table "sales"
transactionId salepersonId amount
1 1 100
2 1 200
3 2 50
4 3 60
5 3 200
I like to find out how to get the salesperson with the highest total sale.
I know I need to use max and sum but I don't know how to combine them.
This is what I got
select salespersonId, sum(amount) from sales group by salesperson;
This would give me the total sales of each person, but I am not sure the next step to get the max. can someone help me ?
The standard SQL way is to use order by and limit or top
select salespersonid, sum(amount) as total
from sales
group by salespersonid
order by total desc
limit 1;
In SQL Server, you would use top 1 instead of limit 1:
select top 1 salespersonid, sum(amount) as total
from sales
group by salespersonid
order by total desc;
select salespersonId, sum(amount) as total
from sales group by salespersonID ORDER BY total DESC LIMIT 1;
This should work
select salepersonId, sum(amount) from sales group by amount DESC limit 1
OR
select rec.id, max(rec.amt) from (select salepersonId id, sum(amount) amt from sales group by salepersonId) AS rec