Use aggregate functions with group by from several tables - mysql

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

Related

How to find which number of orders for a customer

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;

name and sum from 2 different tables

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;

How can I fix my table to make a expiration of medicine?

I'm doing an inventory system of drug store. And now I'm trying to do the expiration of medicine. I want to show you my table. Maybe you have an idea of how to fix my tables. I put the expiration date in tbl_received_order. When I received a medicine it will insert into tbl_medicine. What if the customer bought 50 pieces of paracetamol or supplier_med_id is equal to 3. And after two months the medicine received will be expired. I use DATE(NOW()) > tbl_received_order.expiration_date to get all the expired medicine.
My problem is that the qty is not tally to rec_qty when the medicine expired. Just for below example, I have 100 pcs. on my initial inventory. But later on, 50 pcs. was sold. Why is it 100 pcs.is still reflecting on my inventory of medicines for expiration, instead of 50pcs. How can I fix my table? Can somebody help me with my problem?
The is my table
tbl_medicine
med_id supplier_med_id qty status branch_id
1 3 50 Active 1
2 7 100 Active 1
3 9 100 Active 1
tbl_received_order
received_id user_id purchase_order_id date_received
RO20190001 1 PO20190001 2019-05-04
tbl_received_order_details
id received_id supplier_med_id rec_qty expiration_date
1 RO20190001 3 100 2019-11-04
2 RO20190001 7 100 2019-11-04
3 RO20190001 9 100 2019-11-04
1 RO20190002 3 50 2019-11-04
2 RO20190002 7 50 2019-11-04
3 RO20190002 9 50 2019-11-04
tbl_transaction
transaction_id customer_id user_id transaction_date branch_id
0001 CU2018001 1 2019-09-04 1
0002 CU2018001 1 2019-09-04 1
0003 CU2018001 1 2019-09-04 1
tbl_transaction_details
details_id transaction_id supplier_med_id qty price total_price
1 0001 3 50 10.00 500
2 0002 3 10 10.00 100
3 0003 3 10 10.00 100
You can get the accurate quantity by using a JOIN.
SELECT orderd.expiration_date, orderd.rec_qty - SUM(trans.qty) AS inventory_left
FROM tbl_received_order_details AS orderd
JOIN tbl_transaction_details AS trans
ON orderd.supplier_med_id = trans.supplier_med_id
WHERE orderd.supplier_med_id = 3
AND orderd.expiration_date < '2019-11-05'
Join tbl_received_order_details and tbl_transaction_details using supplier_med_id.
Filter the records with medicine id '3' and an expiration date.
Since there can be more than 1 transaction for a particular medicine, we need to SUM the quantity sold and subtract it from total quantity to get what's left.
Update 1
Above query will get expired medicines for the ones sold. To get quantity left for all inventories sold, use GROUP BY.
SELECT orderd.expiration_date, orderd.rec_qty - SUM(trans.qty) AS inventory_left
FROM tbl_received_order_details AS orderd
JOIN tbl_transaction_details AS trans
ON orderd.supplier_med_id = trans.supplier_med_id
WHERE orderd.expiration_date < '2019-11-05'
GROUP BY orderd.supplier_med_id
UPDATE 2
To get all left inventories that are expired whether they are sold or not, use LEFT JOIN. COALESCE(trans.qty, 0) will return first NOT NULL value. So if a medicine is never sold, it will not have any entry in transaction table which will return qty as NULL. In such cases, we will subtract zero from rec_qty which in turn will get complete rec_qty as left overs.
SELECT orderd.expiration_date, orderd.rec_qty - SUM(COALESCE(trans.qty, 0)) AS inventory_left
FROM tbl_received_order_details AS orderd
LEFT JOIN tbl_transaction_details AS trans
ON orderd.supplier_med_id = trans.supplier_med_id
WHERE orderd.expiration_date < '2019-11-05'
GROUP BY orderd.supplier_med_id

MySQL join next lower value from other table

I've the two tables orders
id article amount
1 1 1
2 2 50
and prices
id article min_amount price
1 1 1 42.99
2 2 1 5.06
3 2 5 4.55
4 2 10 4.3
5 2 25 4.05
6 2 100 2.66
The prices tables contains IDs of articles and a minimum amount you would have to buy to get a bulk discount (which would change the price for the order). I would like to join prices into orders, so that the result looks like:
id article amount price
1 1 1 42.99
2 2 50 4.05
The order id 2 is above the minimum (25) to get the article for 4.05€, but still below 100 at which you would get a bigger discount, so the query would to have pick the next-lower value.
I've tried this query so far
SELECT
orders.id AS id,
orders.article,
orders.amount,
prices.price,
(orders.amount - prices.min_amount) AS discount_diff
FROM orders
LEFT JOIN prices ON (prices.article = orders.article) AND (prices.min_amount <= orders.amount)
which gives this result
id article amount price discount_diff
1 1 1 42.99 0
2 2 50 5.06 49
2 2 50 4.55 45
2 2 50 4.3 40
2 2 50 4.05 25
You can find this example on "js"fiddle: http://sqlfiddle.com/#!9/1b2bf/8
The query you need is this:
SELECT orders.id AS id,
orders.article,
orders.amount,
prices.price
FROM orders
INNER JOIN prices ON ( prices.article = orders.article
and prices.min_amount <= orders.amount)
INNER JOIN ( SELECT orders.article,
orders.amount,
min(prices.price) minprince
FROM orders
INNER JOIN prices ON (prices.article = orders.article
AND prices.min_amount <= orders.amount)
GROUP BY orders.article,
orders.amount) b
ON ( prices.article = b.article
AND orders.amount = b.amount
AND prices.price = b.minprince)
See it here: http://sqlfiddle.com/#!9/1b2bf/27

how to get total order price using mysql?

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