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
Related
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
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
I have a table ORDERS which has something like this value ,
customerNumber | orderNumber(PK)
40 1
30 2
40 3
20 4
30 5
So, this table has customerNumbers 40 and 30 placing the max orders. Can anyone tell me a MySQL query to return the customerNumber (numbers), i dont want the count of the orders, just want the customer (cutomers) with the max order placed .
Thanks.
You can use below statement to get the Customer who placed maximum orders.
SELECT customerNumber FROM orders
GROUP BY customerNumber
ORDER BY COUNT(orderNumber) DESC LIMIT 1;
I should get deservedly flamed for this, but hey, the sun's out and it's feeling like a good day...
SELECT x.customernumber
FROM
( SELECT customernumber
, COUNT(*) total
FROM my_table
GROUP
BY customernumber
) x
JOIN
( SELECT COUNT(*) total
FROM my_table
GROUP
BY customernumber
ORDER
BY total DESC
LIMIT 1
) y
ON y.total = x.total;
I facing an issue of a way of calculating the total of the produced Amount column, so I can use it to calculate the
(user sales amount/total users sales amount) percentage ratio
SELECT * FROM Sales
ID Item Amount
5 IS1 10.00
5 IS2 2.00
3 IS1 10.00
2 IS3 7.00
9 IS5 6.00
Example 35.00
DESIRED OUTPUT
ID Amount Percentage
5 12.00 34.28571%
3 10.00 28.57142%
2 7.00 20.00000%
9 6.00 17.14285%
Example 35.00 100.0000%
SELECT ID, SUM(Amount) AS Amount
FROM Sales
GROUP BY ID
I have seen various examples on stack and other forums where people are using an additional select to calculate the total of amount column as such
SELECT ID, SUM(Amount) AS Amount,(SELECT SUM(Amount) FROM Sales) AS Total, Amount/(SELECT SUM(Amount) FROM Sales)*100 AS Percentage
FROM Sales
GROUP BY ID
But what do I do when my select query is huge, do you still put it in the select query or is there a better way this can be done.
SELECT a.ID, SUM(a.Amount) AS Amount, #rank:=#rank + 1 AS rank
FROM Sales a
INNER JOIN (SELECT #rank:=0) r
WHERE a.Item='IS1'
AND a.tdate='2015-01-20'
GROUP BY a.ID
ORDER BY Amount
The original query is actually longer than this, because it is also pulling in descriptions and values from other tables.
Essentially the original code of what I am doing, is going to the Sales table, pulling in all user sales data by current day, by product, by product sub_id, by transaction id. Then ranking who sold the highest amount of product, on the day, of a particular transaction id.
Of course this code gets huge, so I am not sure if it is right to add this whole code again in SELECT query to calculate a total?
Is there no way, it can calculate the total of amount without doing another query?
This will work on MSSQL, not sure about MySQL
Select distinct ID, SUM(AMOUNT) OVER (PARTITION BY ID) Amount, SUM(AMOUNT) OVER (PARTITION BY ID) / SUM(AMOUNT) OVER (PARTITION BY 1) * 100 Percentage
from Sales
order by ID
It provides you with your Desired Output as shown above.
try
declare #total_amt as decimal(18,5)
set #total_amt=(select sum(isnull(AMOUNT,0)) from Sales)
select Id,AMOUNT,(SUM(Amount)/(#total_amt)*100) as Percentage from Sales group by Id
I prefere a group by over a window function if it solves my problem:
SELECT a.ID, SUM(a.Amount) AS Amount, #rank:=#rank + 1 AS rank
, s.SumAll Total
, SUM(a.Amount)/s.SumAll*100 Percentage
FROM Sales a
, (SELECT #rank:=0) r
, (SELECT SUM(Amount) SumAll FROM Sales ) s
WHERE a.Item='IS1'
AND a.tdate='2015-01-20'
GROUP BY a.ID
ORDER BY Amount
Assuming sales is in reality a rather expensive view, you'll maybe better of with that (asuming mysql likes window functions):
SELECT a.ID, SUM(a.Amount) AS Amount
, #rank:=#rank + 1 AS rank
, SUM(Amount) OVER (Partition By 1) Total
, SUM(a.Amount)/SUM(Amount) OVER (Partition By 1)*100 Percentage
FROM Sales a
, (SELECT #rank:=0) r
WHERE a.Item='IS1'
AND a.tdate='2015-01-20'
GROUP BY a.ID
ORDER BY Amount
This query produces the desired output without a sub query in MySQL. SUM(Amount) OVER () gives you the total sum of Amount
SELECT ID, SUM(Amount) AS Amount,
SUM(Amount)*100/SUM(Amount) OVER () AS percentage
FROM Sales
GROUP BY ID
I have this table :
Orders(Orderid,CustomerID,Amount)
I ask id's of top 10 customers (total amount of order)
I wrote the query which is print all customer (highest amount to min amount)
select CustomerID, sum(Amount) as Total from orders group by CustomerID order by Total desc;
How can I get the first 10 line of this result ? Or is my way to obtain top 10 wrong?
Here is what you want:
select CustomerID, sum(Amount) as Total
from orders
group by CustomerID
order by Total desc
LIMIT 10;