Select based on value of another select - mysql

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';

Related

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`

Use COALESCE within SUM in MySQL SELECT statement

I have the following tables:
Table users
id name base_discount
1 jack 10
2 michael 20
3 richard 30
Table item
id name category_id price
1 hammer 1 10
2 knife 2 15
3 spoon 2 12
4 plate 3 20
5 tree 4 400
Table category
id name
1 tools
2 kitchen
3 dishes
4 garden
Table discount_category
id user_id category_id discount
1 1 1 20
2 1 3 25
3 3 3 10
4 1 2 15
Table discount_item
id user_id item_id discount
1 2 1 50
2 1 2 50
Now what I want to achieve. I want to attach the discount per item that a user has to the correct item. If that is not available (NULL) I want to attach the discount per category that a user has. And if that is not available (NULL), I want to attach the base discount that a user has. With the discount I then calculate the new price of the item. However, when I try using COALESCE() within SUM() I get a syntax error. What am I doing wrong?
Below is my current query:
SELECT item.id, item.name, category.id,
category.name AS category_name, item.price, SUM((100 -
COALESCE(
(
SELECT discount_item.discount
FROM discount_item
INNER JOIN users ON discount_item.user_id = users.id
WHERE users.id = '1' AND discount_item.item_id = item.id
),
(
SELECT discount_category.discount
FROM discount_category
INNER JOIN users ON discount_category.user_id = users.id
WHERE users.id = '1' AND discount_category.category_id = item.category_id
),
(
SELECT users.base_discount
FROM users
WHERE users.id = '1'
)
)) / 100 * item.price) AS new_price,
FROM item
INNER JOIN category ON item.category_id = category.id;
Please also see the below link for an SQL Fiddle (couldn't do it on sqlfiddle.com as it wouldn't load). In the example I have appended a suffix to each table name.
http://rextester.com/LCCKSD59098
You have an extra comma after new_price and before FROM ITEM, hence the error.
Rextester Demo
Do not select other columns in select if you are not using them in group by, as in other relational RDBMS, you will get error.
Also use alias for table names for better readibily and to avoid confusion.

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;

mysql extra count field for each row

I have a query here, anyone can help me to count the total duplicated fields?
SELECT *
FROM item
INNER JOIN itemgroup on item.itemgroupid = itemgroup.itemgroupid
INNER JOIN status on status.statusid = item.status
INNER JOIN owner on owner.ownerid = item.owner
INNER JOIN
(
SELECT code //, (SELECT count(*) FROM item WHERE ....) as 'total_duplicateds'
FROM item
GROUP BY code
HAVING count(code) > 1
) dup ON item.code = dup.code
Total items: 500
Total items with duplicated codes: 149
Now I get a total of 149 fields returned, how can I add this as a new field to each row?
After the slash is how I learnt to do it but this is a little higher level for me..
Can someone help me out?
To be even more specific
What I'd like to get returned is like:
itemid | code| itemname | itemgroup | owner | total_duplicateds
1 1000 X 1 1 3
2 1000 X 2 2 3
3 1001 A 1 1 3
4 1000 B 3 1 3
5 1002 U 2 1 3
Add COUNT aggregation and GROUP BY all columns that are interesting you.