there are two tables one is order and 2nd one is order_details respectively,
order table
order_id order_name
1 shoes
2 wallet
3 socks
4 bats
order_details table
order_details_no order_id(foregin key) order_price
1 1 25
2 1 55
3 2 65
4 4 30
5 4 60
My question is, I want result set which includes order_id, order total price in ascending order (eg order 1 total is 80,order 4 total is 90 )
How to get this ?
select order_id,
sum(order_price) as total_sum
from order_details
group by order_id
order by total_sum asc
Select
order.order_name,
sum(order_details.order_price) as price
from order
join order_details
on order_details.order_id=order.order_id
group by
order.order_id
order by
price desc
Related
Given a table orders
id
customer_id
created_at
1
1
2022-09-01
2
2
2022-09-02
3
1
2022-09-03
4
1
2022-09-04
5
2
2022-09-04
How do I produce a column that describes which number in the series for the customers the order is?
Example
id
customer_id
created_at
order number
1
1
2022-09-01
1
2
2
2022-09-02
1
3
1
2022-09-03
2
4
1
2022-09-04
3
5
2
2022-09-5
2
You can use a window function for that. With a cumulative count over a partition by customer id, you get exactly the order number you need:
select orders.*,
count(*) over (partition by customer_id order by id) order_number
from orders
order by id;
In MySQL 5.7 you could do this:
select customer_id,
(select count(*)
from orders
where customer_id = main.customer_id and id <= main.id)
from orders main;
I have 2 tables.
table customer have. id , name , age
table order have . id, customer_id , order_amount , order date.
I want to show all name from customer table and sum of order amount from order table according to customer.
customer_id
Name
age
1
Alice
24
2
Bob
52
3
Carol
45
4
Dave
51
order_id
customer_id
order_amount
order_date
1
2
50
2012-4-5
2
1
27
2012-8-1
3
2
12
2013-5-20
4
4
25
2014-1-25
5
4
30
2014-5-30
6
1
20
2014-6-22
EDIT
I tried this but it gives me only bob and sum of all columns instead of separate sum of customers
SELECT customers.name, SUM(orders.order_amount) FROM `orders` INNER JOIN customers WHERE orders.customer_id = customers.customer_id;
Joining condition must be on ON clause, not in WHERE.
You must specify for what group the sum must be calculated.
SELECT customers.name, SUM(orders.order_amount)
FROM `orders`
INNER JOIN customers ON orders.customer_id = customers.customer_id
GROUP BY customers.name;
I have the following tables to allow the subscriber to sell products through the application
Order Table
OrderId
Date
1
2021-07-10
2
2021-08-24
Approval table
ApprovalId
OrderId
Status
SellerId
1
1
Accepted
10
2
1
Rejected
20
3
2
Accepted
30
Item table
ItemId
OrderId
Price
Qty
SellerId
1
1
620$
1
10
2
1
150$
2
10
3
1
410$
1
20
4
2
220$
1
30
what i want is to display the income revenue for > only who accept the order
Date
Sales
Seller_Part 90%
Net_Sales 10%
2021-07-10
770$
693$
77$
2021-08-24
220$
198%
22$
I tried using aggregate functions with group by but the result include rejected order also
select o.date
,sum(i.price * i.qty) sales
,sum(i.price * i.qty) * 0.90 Seller_Part90%
,sum(i.price * i.qty) * 0.10 Net_Sales10%
from Order o
join Approval a
on o.orderId = a.OrderId
and a.status= 'Accepted'
join Items i
on a.sellerid = i.sellerid
and a.orderid = i.orderid
group by o.date
I have the following Table named Order
Orders Table
___________________________________________________
orderiD | userId | OrderType | Order_Date | Amount
________|________|___________|____________|________
1 1 0 12/12/2009 1
2 1 1 13/12/2009 2
3 1 1 14/12/2009 3
4 2 0 12/12/2009 4
5 2 1 16/12/2009 2
6 1 0 14/12/2009 5
7 2 1 17/12/2009 4
8 2 0 10/12/2010 2
___________________________________________________
I need to create query which returns user id with maximum SUM of purchases.
I tried the following
Select MAX(GRP.sumAmmount), o.userId join
(Select SUM(o.Amount) as sum_ammount, o.userId as UID from Orders GROUP BY(o.userID)) as GRP on o.userId=GRP.UID GROUP BY(GRP.UID)
But I believe I'm missing something.
Can you help?
If I understand your question correctly, you want to return the UserID which has the maximum SUM (total) of purchases. So the above records will result:
UserID Total Amount
2 12
And the simpliest solution would be:
SELECT UserID, SUM(AMOUNT) as TotalAmount
FROM Orders
GROUP BY UserID
ORDER BY TotalAmount DESC
LIMIT 1
I'm ready to edit this if I'm wrong. :-) (PS: Please add your desired result)
Thanks.
UPDATE 1
Derived from you query:
Select MAX(SUM(o.Amount)) as sum_ammount,
o.userId as UID
FROM Orders o
GROUP BY o.userID
ORDER BY sum_ammount DESC
LIMIT 1
See below, its working.
SELECT userId, sum(Amount) as 'Total'
FROM Orders
GROUP BY userId
ORDER BY Total DESC
LIMIT 1
Output is
+++++++++++++++
userId + Total
+++++++++++++++
2 + 12
+++++++++++++++
I also tried after adding new row as
insert into Orders values (9,1,0,'2010-12-10 12:12:12',10)
Output is
+++++++++++++++
userId + Total
+++++++++++++++
1 + 21
+++++++++++++++
I prefer to have this solution
SELECT UserID, SUM(AMOUNT) as TotalAmount
FROM Orders
GROUP BY UserID
having SUM(AMOUNT) = (select sum(amount) as col1
from orders
group by userid
order by col1 desc
limit 1
)
This sql will show all users who have max purchases
Just try to change amount of orderid 1 to be 2 , then this sql will show both.
I have a table of orders. Each order is linked to one or more basket items.
Basic code:
SELECT * FROM Orders o JOIN OrderItems oi ON o.OrderNumber = oi.OrderNumber
I can limit to the last 5 items by doing this:
ORDER BY oi.CreatedDate DESC LIMIT 5
However, in some cases the user has placed multiple orders across the same products. e.g.
OrderNo ItemNo ProductId
1 1 70
1 2 20
2 1 80
2 2 30
3 1 10
4 1 90
5 1 10
6 1 40
7 1 50
8 1 100
9 1 10
10 1 30
11 1 10
12 1 60
If I get the last five items, I'd end up with 60, 10, 30, 10, 100. What I actually want is to get the last 5 unique product IDs - so that'd be 60, 10, 30, 100, 50. What would the SQL for this be?
Edited
If I use GROUP BY I get 60, 100, 50, 40, 90. Where is 30?
Try:
select ProductID from
(select ProductID, max(OrderNo)
from Orders
group by ProductID
order by 2 desc) sq
limit 5
USE `
GROUP BY Product ORDER BY Order DESC LIMIT 5
`
Just add GROUP BY Product
SELECT *
FROM Orders o
JOIN OrderItems oi ON o.OrderNumber = oi.OrderNumber
GROUP BY o.Product
ORDER BY oi.CreatedDate DESC LIMIT 5
EDIT: the problem is that MySql does group by before order by. A trick to fool it is:
SELECT * FROM
(
SELECT
O.OrderNumber,
oi.ItemNo,
oi.Productid
FROM
orders o
JOIN
OrderItems oi ON o.OrderNumber = oi.OrderNumber
ORDER BY
oi.CreateDate DESC
) AS fool_mysql
GROUP BY
ProductId
LIMIT 5;
That is, do the ordering BEFORE the grouping.