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
Related
During my interview, I had a simple sounding question about calculating daily average number of orders and daily average revenue. But in the table, records were arranged like this: If I place an order with 4 items, the 4 items were shown as individual records with the same order ID. But the order_value column had only the total order value in all the 4 records. How can I calculate
The average daily order value and daily average number of orders?
How many orders were delivered within the preparation time of 20 minutes?
Giving the table below for reference:
The way I understand it.
You start by calculating the metrics per day.
select
date(order_date) as order_day
, sum(final_bill) as daily_total_bill
, count(order_id) as daily_orders
, count(case
when timestampdiff(minute, order_date, prepared_date) <= 20
then order_id
end) as daily_ontime
from kitchen_orders
group by date(order_date)
order_day
daily_total_bill
daily_orders
daily_ontime
2020-12-01
95.30
3
3
2020-12-02
105.60
3
2
And then wrap that in a sub-query or cte, to get the averages and totals for all.
select
round(avg(daily_total_bill), 2) as avg_daily_bill,
round(avg(daily_orders), 1) as avg_daily_orders,
sum(daily_ontime) as total_ontime
from
(
select
date(order_date) as order_day
, sum(final_bill) as daily_total_bill
, count(order_id) as daily_orders
, count(case
when timestampdiff(minute, order_date, prepared_date) <= 20
then order_id
end) as daily_ontime
from kitchen_orders
group by date(order_date)
) q
avg_daily_bill
avg_daily_orders
total_ontime
100.45
3.0
5
Demo on db<>fiddle here
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 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
I've been searching all over, but couldn't come up with a proper solution to sort my table 'shop'.
This is how it looks:
Product Price
---------------------------------
Site 1 35
Site 2 50
Site 3 15
Site 1 30
Site 2 5
Now I need it to look like this:
Product Price
---------------------------------
Site 2 50
Site 2 5
Site 1 35
Site 1 30
Site 3 15
The table should be sorted starting with the highest price and then grouping it by the product.
I tried a million different queries and the closest I got was this:
SELECT m.* FROM shop m
INNER JOIN
(SELECT product, MAX(price) AS maxprice FROM shop GROUP BY product ORDER BY maxprice ASC) s
ON m.product = s.product
ORDER BY s.maxprice DESC
The query does it's job but sorts the prices in the group the wrong way around.
Product Price
---------------------------------
Site 2 5
Site 2 50
Site 1 30
Site 1 35
Site 3 15
What am I doing wrong? Help is much appreciated!
Best and thanks a million!
Select x.product, x.price from
(Select product, max(price) as mprice from shop
group by product) as tbl inner join shop x on tbl.product = x.product
order by tbl.mprice desc, x.Price desc
I also notice you created a fiddle would have saved me some time but here is the update fiddle
SELECT s.product, s.Price
from (Select product, max(price) as mprice
from shop group by product) as tbl
inner join shop s on s.product = tbl.product
order by tbl.mprice desc, s.price desc
http://sqlfiddle.com/#!2/c5eb64/3
You've got two levels of sorting, so you need to describe both in your ORDER BY
ORDER BY s.maxprice DESC, m.price DESC
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