mysql select left join multi tables sum & group by gives repeated answer - mysql

I have three tables
Table 1 : item
id item
5 pen
6 pencil
Table 2: purchase
id item qty item_id
1 pen 10 5
2 pencil 10 6
3 pen 10 5
Table 3: sale
id item qty item_id
1 pen 5 5
I want the result as follows :
Required result :
item purchase sale stock
pen 20 5 15
pencil 10 0 10

You may try below query -
SELECT P.item, P.purchase, IFNULL(S.sale, 0), P.purchase - IFNULL(S.sale, 0) stock
FROM (SELECT item, SUM(qty) purchase
FROM purchase
GROUP BY item) P
LEFT JOIN (SELECT item, SUM(qty) sale
FROM sale
GROUP BY item) S ON P.item = S.item

Related

MySQL - Select lowest price that has quantity, if none have quantity, select the lowest price

I have a table with product_id, warehouse, quantity, and price. My goal is to select the lowest price for each product. However, I want to choose the record with the lowest price that has a quantity, unless they are all 0 in which case I want the record with just the lowest price.
Example
product_id
warehouse
quantity
price
1
A
5
14
1
B
0
12
1
C
8
17
2
A
0
12
2
C
0
10
3
D
3
12
After the query, it should return a table such as this:
product_id
warehouse
quantity
price
1
A
5
14
2
C
0
10
3
D
3
12
My initial thought is to do some kind of inner join on price = min(price). I know how to get the record with the min price but can't wrap my brain around having it dependent on the quantity as well.

MySQL find MIN() from duplicate rows

I have 3 tables like below:
Product Table
PID CODE
1 a
2 b
Price Table
PrID PID(ID from Product) UMID(ID from UOM) Price
1 1 1 10
2 1 2 5
3 2 1 10
UOM Table
UMID UM_NAME UM_RATE
1 BOX 5
2 PC 10
I want to get product with price and that uom which has min UM_RATE. For example, product CODE a has two prices but the prices are different for different uom. What I want to achieve is,
PID CODE PrID Price UM_NAME UM_RATE
1 a 1 10 BOX 5
2 b 3 10 BOX 5
Because box has min UM_RATE. I have tried the following query,
SELECT product.*, price.*, uom.um_name, MIN(uom.um_rate)
FROM product
LEFT JOIN price ON price.pid = product.pid
LEFT JOIN uom ON price.umid = uom.umid
GROUP BY product.pid
This query gives me the following result,
PID CODE PrID Price UM_NAME UM_RATE
1 a 1 10 PC 5
2 b 3 10 BOX 5
which is wrong. because UM_RATE 5 is belong to UM_NAME box.
How can I get the expected result?
use join and corelated subquery
select * from (select distinct p.*,u.UMID as uid,
u.UM_NAME,u.UM_RATE,pr.code from
price p join uom u on p.UMID=u.UMID
join product pr on pr.PID=p.PID
) a
where a.UM_RATE =(select min(UM_RATE) from
(select p.*,u.UMID as uid,
u.UM_NAME,u.UM_RATE,pr.code from
price p join uom u on p.UMID=u.UMID
join product pr on pr.PID=p.PID) t where t.code=a.code
)
prid PID UMID price uid UM_NAME UM_RATE code
1 1 1 10 1 Box 5 a
3 2 1 10 1 Box 5 b

How to get right sum from inner join in mysql

I have two tables one is orders and second is order_product in which I have to find out orders count, product count, totalamount in corresponding to store using store id from which I have successfully find out the orders count and product count but my totalamount is not coming correct.
orders:
...........................
order_id or_total_amt
...........................
1 10
2 10
3 10
order_product
.................................
op_id op_order_id st_id
.................................
1 1 1
2 2 2
3 3 1
4 3 1
I want below output but my totalamount value is coming wrong it is coming 30,but the correct value is 20 which i have mentioned in the right output below.
output which i want:
.........................................
st_id orders product totalmount
.........................................
1 2 3 20
2 1 1 10
I have tried the below query which is giving 30 value of totalamount which is wrong.
SELECT `op_st_id`,count(distinct orders.`order_id`)as orders,count(order_product.op_pr_id) as product
,sum(orders.or_total_amt) as totalamount from orders
inner JOIN order_product on orders.order_id=order_product.op_order_id
group by `op_st_id`
SELECT
`st_id`,
count(DISTINCT orders.`order_id`) AS orders,
count(order_product.op_id) AS product,
count(DISTINCT orders.`order_id`)*(sum(orders.or_total_amt)/count(order_product.op_id)) AS totalamount
FROM
orders
INNER JOIN order_product ON orders.order_id = order_product.op_order_id
GROUP BY
`st_id`

Sum delivered values and subtract it from the received values with the same name mysql

I have this table
table name: com_inv
item_name amount date_added
item 1 1 06/06/2015
item 2 2 07/06/2015
item 3 3 08/06/2015
item 1 10 09/06/2015
item 2 20 10/06/2015
item 3 30 11/06/2015
table name: sls_ordrs
item_name amount order_status date_received
item 1 1 received 06/06/2015
item 2 1 delivered
item 3 2 received 08/06/2015
item 1 5 received 09/06/2015
item 2 5 delivered
item 3 2 received 11/06/2015
What I want to achieve is, per item, subtract the sum of the amount in sls_ordrs that have been "received" from the sum of the amount in com_inv. The resulting table should be like this:
Item Name Stocked Dispensed Remaining
item 1 11 6 5
item 2 22 0 22
item 3 33 4 29
If I use SQL Server, I would just use the CTE but with MySql I need your help. This is the code I came up so far...
SELECT
a.item_name,
b.stocked AS 'Stocked',
sum(a.amount) AS 'Dispensed',
IFNULL(b.stocked, 0) - IFNULL(a.amount, 0) AS 'Remaining'
FROM
sls_ordrs a
LEFT JOIN (
SELECT
item_name AS 'item_name',
SUM(amount) AS 'stocked'
FROM
com_inv
GROUP BY
item_name
) b ON a.item_name = b.item_name
WHERE
a.order_status = 'received'
GROUP BY
item_name
The stocked column here gives me a null value.
Use this Query. Hope it should be working fine.
SQL FIDDLE DEMO
SELECT
I.item_name,
I.Stocked,
S.Dispensed,
COALESCE (I.Stocked, 0) - COALESCE (S.Dispensed, 0) AS Remaining
FROM
(
SELECT
item_name AS 'item_name',
SUM(amount) AS 'Stocked'
FROM
com_inv
GROUP BY
item_name
) I
LEFT JOIN (
SELECT
item_name,
order_status,
SUM(amount) AS Dispensed
FROM
sls_ordrs
WHERE
order_status = 'received'
GROUP BY
item_name
) S ON I.item_name = S.item_name;

Select based on value of another select

I have two tables that like this:
'BUDGET TABLE'
idBudget BudgetDescription CreateDate idProject
1 NameBudget1 09/09/2015 2
2 NameBudget2 08/07/2015 1
3 NameBudget3 08/09/2015 1
'ITEMS' the items in every budget
idItem ItemDescription Price IdBudget
1 Item1 10 1
2 Item2 30 1
3 Item3 5 2
4 Item4 130 3
5 Item5 27 3
And I'm trying to get these kind of results if the USER wants to see the budgets that belong to Project = '1'
-----------------------------------------------
BudgetDescription CreateDate Sum(Price) as Total
----------------------------------------------
NameBudget2 08/07/2015 5
NameBudget3 08/09/2015 157
-----------------------------------------------
I tried to join these querys:
Select idBudget, BudgetDescripcion, CreateDate
from budget
where idProject='1';
Select sum(price) as total
from Items i
where i.idBudget=idBudget;
like
Select sum(price) from item where idBudget=(select idBudget, BudgetDescription, createdate from Budget where idProject='1');
but thats gives me an error: "Operand shoul contain 1 column"
so I tried and erase the other columns:
Select sum(price) from item where idBudget=(select idBudget from Budget where idProject='1');
and error: Subquery returns more than 1 row
Thank you very much for your help.
select
b.idBudget,
b.BudgetDescripcion,
b.CreateDate BudgetCreateDate,
COUNT(i.idItem) CountItems,
SUM(i.price) SumItemPrice
from
budget b
inner join Items i on i.IdBudget = b.idBudget
where
b.idProject = 1
group by
b.idBudget, b.BudgetDescripcion, b.CreateDate;
LEFT JOIN is what you seek.
Select b.idBudget, b.BudgetDescripcion, b.CreateDate, sum(i.price) as total
from budget b
LEFT JOIN Items i ON i.idBudget=b.idBudget
where b.idProject='1';