Multiplying quantity and amount in different tables in same database - mysql

these are my tables
ORDER_ITEMS
+----------+--------+-----------------+------------+
| order_id |item_id | item_name | quantity |
+----------+--------+-----------------+------------+
| 1 | 1 | coffee | 2 |
| 2 | 2 | shake | 2 |
| 2 | 3 | icecream | 3 |
+----------+--------+-----------------+------------+
PRODUCT_INGREDIENT:
+--------+-----------------+--------+
|item_id | ingredient_id | amount |
+--------+-----------------+--------+
| 1 | 123 | 10 |
| 1 | 124 | 15 |
| 2 | 125 | 10 |
| 2 | 124 | 15 |
| 2 | 123 | 10 |
| 2 | 126 | 15 |
| 3 | 124 | 15 |
| 3 | 123 | 10 |
| 3 | 126 | 15 |
+--------+-----------------+--------+
INVENTORY:
+--------+-----------------+--------+
| id | ingredient_id | amount |
+--------+-----------------+--------+
| 1 | 123 | 80 |
| 2 | 124 | 70 |
| 3 | 125 | 100 |
| 4 | 126 | 100 |
+--------+-----------------+--------+
the sql I have is working but I need to multiply the (amount)column in PRODUCT_INGREDIENT table to the column(quantity) in ORDER_ITEMS table
before
this is the sql statement
UPDATE inventory i
INNER JOIN (
SELECT p.ingredient_id, sum(p.amount) amount
FROM product_ingredient p
INNER JOIN order_items o on o.item_id = p.item_id
WHERE o.order_id = 1
GROUP BY p.ingredient_id
) p ON i.ingredient_id = p.ingredient_id
SET i.amount = i.amount - p.amount
I want my inventory to look like this after query
INVENTORY:
+--------+-----------------+--------+
| id | ingredient_id | amount |
+--------+-----------------+--------+
| 1 | 123 | 100 |
| 2 | 124 | 100 |
| 3 | 125 | 100 |
| 4 | 126 | 100 |
+--------+-----------------+--------+

Looking to your data sample seems you need add the result of the subquery
UPDATE inventory i
INNER JOIN (
select p.ingredient_id, sum(p.amount*o.quantity) amount
from product_ingredient p
INNER JOIN order_items o on o.item_id = p.item_id
WHERE o.order_id = 1
GROUP BY p.ingredient_id
) p ON i.ingredient_id = p.ingredient_id
SET i.amount = i.amount + p.amount

You can use such an UPDATE statement :
UPDATE inventory i
INNER JOIN (
SELECT p.ingredient_id, sum(p.amount*o.quantity) amount
FROM product_ingredient p
INNER JOIN order_items o on o.item_id = p.item_id
WHERE o.order_id = 1
GROUP BY p.ingredient_id
) p ON i.ingredient_id = p.ingredient_id
SET i.amount = i.amount + p.amount
Demo
Only ingredient_id 123 and 124 are matched for the whole query. So, only they're updated.

Related

MySQL query with sum fields from other table, with a twist

Sorry for the vague title, but I don't know how to word this type of problem better. Here is a simple example to explain it. I have to tables: OrderItemList and OrderHistoryLog.
OrderItemList:
|------------------------------|
| OrderNo | ItemNo | Loc | Qty |
|------------------------------|
| 100 | A | 1 | 1 |
| 101 | A | 1 | 2 |
| 102 | A | 1 | 1 |
| 103 | A | 2 | 1 |
| 104 | A | 2 | 1 |
OrderHistoryLog:
|------------------------------|
| OrderNo | ItemNo | Loc | Qty |
|------------------------------|
| 50 | A | 1 | 5 |
| 51 | A | 1 | 2 |
| 100 | A | 1 | 1 |
| 102 | A | 1 | 3 |
| 103 | A | 2 | 1 |
I need to show the records in the OrderItemList along with a LocHistQty field, which is the sum(Qty) from the OrderHistoryLog table for a given Item and Location, but only for the orders that are present in the OrderItemList.
For the above example, the result should be:
Result:
|------------------------------------------------------
| OrderNo | ItemNo | Loc | Qty | HistQty | LocHistQty |
|------------------------------|-----------------------
| 100 | A | 1 | 1 | 1 | 4 |
| 101 | A | 1 | 2 | 0 | 4 |
| 102 | A | 1 | 1 | 3 | 4 |
| 103 | A | 2 | 1 | 1 | 1 |
| 104 | A | 2 | 1 | 0 | 1 |
It is the last field, LocHistQty that I could use some help with. Here is what I started with (does not work):
select OI.OrderNo, OI.ItemNo, OI.Loc, OI.Qty, IFNULL(OL.Qty, 0) as HistQty, OL2.LocHistQty
from OrderItemList OI
left join OrderItemLog OL on OL.OrderNo = OI.OrderNo and OL.ItemNo = OI.ItemNo
join
(
select ItemNo, Loc, sum(qty) as LocHistQty
from zOrderItemLog
group by ItemNo, Loc
) as OL2
on OL2.ItemNo = OI.ItemNo and OL2.Loc = OI.Loc
order by OrderNo
The issue is with the above SQL is that LocHistQty contains the summary of the Qty for all orders (=11 for Loc 1 and 1 for Loc 2), not only the ones in OrderItemList.
Lastly, the real data is voluminous and query performance is important.
Help would be much appreciated.
The subquery can join with OrderItemList to restrict the order numbers that it sums.
select OI.OrderNo, OI.ItemNo, OI.Loc, OI.Qty, IFNULL(OL.Qty, 0) as HistQty, OL2.LocHistQty
from OrderItemList OI
left join OrderItemLog OL on OL.OrderNo = OI.OrderNo and OL.ItemNo = OI.ItemNo
join
(
select OL.ItemNo, OL.Loc, sum(OL.qty) as LocHistQty
from OrderItemLog AS OL
JOIN OrderItemList AS OI ON OL.OrderNo = OI.OrderNo
group by OL.ItemNo, OL.Loc
) as OL2
on OL2.ItemNo = OI.ItemNo and OL2.Loc = OI.Loc
order by OrderNo
DEMO
Option 1
SELECT
OrderNo,
ItemNo,
Loc,
Qty,
(SELECT
Qty
FROM
OrderHistoryLog AS A
WHERE
A.OrderNo = B.OrderNo AND A.Loc = B.Loc) AS HistQty,
(SELECT
SUM(Qty)
FROM
OrderHistoryLog AS D
WHERE
D.OrderNo = B.OrderNo AND D.Loc = B.Loc) AS LocHistQty
FROM
OrderItemList AS B;
Option 2
SELECT
B.OrderNo,
B.ItemNo,
B.Loc,
B.Qty,
C.Qty AS HistQty,
(SELECT
SUM(Qty)
FROM
OrderHistoryLog AS A
WHERE
A.OrderNo = B.OrderNo AND A.Loc = B.Loc) AS LocHistQty
FROM
OrderItemList AS B,
OrderHistoryLog AS C
WHERE
C.OrderNo = B.OrderNo AND C.Loc = B.Loc;

Get results from last WHERE condition

I have the following DB design.
PRODUCTS
+----+----------------------------------+
| id | name |
+----+----------------------------------+
| 1 | Product 1, Intel, Celeron, N3350 |
| 2 | Product 2, Intel, Celeron, N3350 |
| 3 | Product 3, Intel, i5, 8250U |
| 4 | Product 4, Intel, i9, 8950HK |
| 5 | Product 5, Intel, i9, 8950HK |
+----+----------------------------------+
ATTRIBUTES
+----+--------------+
| id | name |
+----+--------------+
| 11 | Manufacturer |
| 22 | Type |
| 33 | Model |
+----+--------------+
ATTRIBUTE_VALUES
+-----+---------+
| id | value |
+-----+---------+
| 111 | Intel |
| 222 | Celeron |
| 333 | i5 |
| 444 | i9 |
| 555 | N3350 |
| 666 | 8250U |
| 777 | 8950HK |
+-----+---------+
And finally, the table that links all of these:
PRODUCT_ATTRIBUTES_VALUES
+------------+--------------+--------------------+
| product_id | attribute_id | attribute_value_id |
+------------+--------------+--------------------+
| 1 | 11 | 111 |
| 1 | 22 | 222 |
| 1 | 33 | 555 |
| 2 | 11 | 111 |
| 2 | 22 | 222 |
| 2 | 33 | 555 |
| 3 | 11 | 111 |
| 3 | 22 | 333 |
| 3 | 33 | 666 |
| 4 | 11 | 111 |
| 4 | 22 | 444 |
| 4 | 33 | 777 |
| 5 | 11 | 111 |
| 5 | 22 | 444 |
| 5 | 33 | 777 |
+------------+--------------+--------------------+
All of these are about filter products.
I want to return the results for each attribute filter.
If I filter by:
Attribute 'Intel' => it should return all 5 results.
Type 'Celeron' => it should return 2 results (only Celeron)
SQL-FIDDLE
SELECT products.id, products.name FROM `products` LEFT JOIN
`product_attributes_values` ON
`product_attributes_values`.`product_id`=`products`.`id` LEFT JOIN
`attributes` ON
`attributes`.`id`=`product_attributes_values`.`attribute_id` LEFT JOIN
`attribute_values` ON
`attribute_values`.`id`=`product_attributes_values`.`attribute_value_id`
WHERE ((attributes.id = 11 AND `attribute_values`.`id` IN (111)) OR
(`attributes`.`id` = 22 AND `attribute_values`.`id` IN (222)) ) GROUP
BY `products`.`id` LIMIT 10
try this one
SELECT
products.id,
products.name
FROM
`products`
LEFT JOIN `product_attributes_values`
ON `product_attributes_values`.`product_id`=`products`.`id`
LEFT JOIN `attributes`
ON `attributes`.`id`=`product_attributes_values`.`attribute_id`
LEFT JOIN `attribute_values`
ON `attribute_values`.`id`=`product_attributes_values`.`attribute_value_id`
WHERE
attributes.id = 11
OR attributes.id = 22
GROUP BY
products.id,
products.name
HAVING
MAX(CASE WHEN attributes.id = 11 THEN attribute_values.id ELSE NULL END) IN (111)
AND MAX(CASE WHEN attributes.id = 22 THEN attribute_values.id ELSE NULL END) IN (222)
LIMIT
10
if you know all your ids, then you don’t need all these joins
SELECT
p.id,
p.name
FROM
`products` as p
LEFT JOIN `product_attributes_values` as pav1
ON p.id = pav1.product_id
and pav1.attribute_id = 11
LEFT JOIN `product_attributes_values` as pav2
ON p.id = pav2.product_id
and pav2.attribute_id = 22
WHERE
pav1.attribute_value_id = 111
AND pav2.attribute_value_id = 222
It is kind of a relational division problem, here is one solution:
SELECT products.id, products.name
FROM products
WHERE EXISTS (
SELECT 1
FROM product_attributes_values
INNER JOIN attributes ON product_attributes_values.attribute_id = attributes.id
INNER JOIN attribute_values ON product_attributes_values.attribute_value_id = attribute_values.id
WHERE product_attributes_values.product_id = products.id AND (
(attributes.attribute_name = 'Manufacturer' AND attribute_values.value = 'Intel®') OR
(attributes.attribute_name = 'Type' AND attribute_values.value = 'Celeron®')
)
HAVING COUNT(*) = 2 -- this must match the number of conditions
)

How to combine table records with some condition in SQL table?

I have six tables in the database, all tables are relative to each other and want to show records in one table.
Following are my tables:
1) mls_stores
*----------------------------*
| store_id | store_title |
*----------------------------*
| 1001 | ajmar-jaipur |
| 1002 | dwarka-delhi |
*----------------------------*
2) mls_category
*-------------------------------------------*
| cat_no | store_id | cat_value | cat_type |
*-------------------------------------------*
| 20 | 1001 | 1 | running |
| 21 | 1001 | 4 | cycling |
| 22 | 1002 | 1 | running |
| 23 | 1002 | 2 | swmining |
*-------------------------------------------*
3) mls_points_matrix
*----------------------------------------*
| store_id | value_per_point | maxpoint |
*----------------------------------------*
| 1001 | 1 | 10 |
| 1001 | 2 | 20 |
| 1002 | 1 | 20 |
| 1002 | 4 | 30 |
*----------------------------------------*
4) mls_user
*--------------------------*
| id | store_id | name |
*--------------------------*
| 1 | 1001 | sandeep |
| 2 | 1001 | jagveer |
| 3 | 1002 | gagan |
*--------------------------*
5) bonus_points
*---------------------------------------------------*
| user_id | store_id | bonus_points | bonus_type |
*---------------------------------------------------*
| 1 | 1001 | 10 | fixed |
| 3 | 1002 | 2 | % |
*---------------------------------------------------*
6) mls_entry
*-------------------------------------------------------*
| user_id | store_id | category | distance | status |
*-------------------------------------------------------*
| 1 | 1001 | 20 | 10 | approved |
| 1 | 1001 | 21 | 40 | approved |
| 1 | 1001 | 20 | 5 | reject |
| 2 | 1001 | 21 | 40 | approved |
| 3 | 1002 | 22 | 10 | approved |
| 3 | 1002 | 23 | 20 | approved |
*-------------------------------------------------------*
Now I want output as below:
*-----------------------------------------------------------------------------------*
| Name | Entries | Points Earned | Bonus Points | Total Points | Total Amount |
*-----------------------------------------------------------------------------------*
| Sandeep | running(1) | 20 | 10 | 30 | 60 |
| | cycling(1) | | | | |
*-----------------------------------------------------------------------------------*
| Jagveer | cycling(1) | 10 | 0 | 10 | 10 |
*-----------------------------------------------------------------------------------*
I am using following code:
SELECT
u.name,
ROUND(COALESCE(t1.points, 0)) AS points,
ROUND(COALESCE(b.bonus_points, 0)) AS bonus_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) AS total_points
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, e.status, SUM(e.distance / c.cat_value) AS points
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
GROUP BY e.user_id
HAVING e.status='approved'
) t1
ON u.id = t1.user_id
LEFT JOIN bonus_points b
ON u.id = b.user_id
WHERE u.store_id = '1001'
ORDER BY
total_points DESC
This SQL query giving me Point earned, bonus points and total points, But I am not able to find Entries And Total Amount and it is giving me wrong Point calculation for Sandeep, As per data one entry is rejected. so it should be 20, not 25.
My total amount will be for Sandeep 30X2(it is coming from point matrix) = 60
same like for jagveer, the total amount for jagveer 10X1 = 10.
I have created tables in DEMO
Try below :
SELECT
u.name,
ROUND(COALESCE(t1.points, 0)) AS points,
ROUND(COALESCE(b.bonus_points, 0)) AS bonus_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) AS total_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) * t1.countId as total_amount,
group_concat(t1.EntriesConcat) as Entries
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, e.status, SUM(e.distance / c.cat_value) AS points,
concat(c.cat_type, '(',count(e.user_id), ')' ) as EntriesConcat,
count(e.user_id) as countId -- it returns count of records according to group by part
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
-- remove HAVING and use WHERE clause
WHERE e.status='approved'
GROUP BY e.user_id
) t1 ON u.id = t1.user_id
LEFT JOIN bonus_points b ON u.id = b.user_id
WHERE u.store_id = '1001'
ORDER BY total_points DESC
group_concat of mysql is useful to concatinating values on Group By

SQL Query: 3 tables highest Value plus rest of row plus

i'm currently stuck on a sql-query, trying to find a solution, but making me headache for 2 stays now. i've got 3 tables
user-table:
+-----+----------+-----------+
| pid | username | role |
+-----+----------+-----------+
| 1 | user1 | patient |
| 2 | user2 | patient |
| 3 | user3 | doc |
| 4 | user4 | assistant |
| 5 | user5 | patient |
+-----+----------+-----------+
base-dat:
+-----+---------+-------+------------+
| pid | surname | name | birthdate |
+-----+---------+-------+------------+
| 1 | smith | john | 1950-07-31 |
| 2 | jackson | sarah | 1948-08-15 |
+-----+---------+-------+------------+
med-dat:
+-----+-----+---------------+--------+--------+
| mid | pid | dateLastEntry | weight | pulse |
+-----+-----+---------------+--------+--------+
| 1 | 1 | 2017-12-01 | 86 | 65 |
| 2 | 1 | 2017-12-02 | 84 | 70 |
| 3 | 1 | 2017-12-03 | 80 | 67 |
| 4 | 2 | 2017-11-15 | 66 | 60 |
| 5 | 2 | 2017-11-17 | 60 | 64 |
+-----+-----+---------------+--------+--------+
I'm trying to get the max(dateLastEntry) for each user with role patient, showing their pid, name, surname, weight, pulse in a single row - , even if there is no med-data entry for the patient: something like this:
+-----+---------+-------+------------+--------+-------+
| pid | surname | name | lastEntry | weight | pulse |
+-----+---------+-------+------------+--------+-------+
| 1 | smith | john | 2017-12-02 | 84 | 70 |
| 2 | jackson | sarah | 2017-11-17 | 60 | 64 |
| 5 | NONE | NONE | NONE | NONE | NONE |
+-----+---------+-------+------------+--------+-------+
Atm my statement looks like this, but can't get the proper result:
select b.pid, s.surname, s.name, max(m.date) as lastEntry, m.weight, m.pulse
from users b
left join med-dat m on b.pid = m.pid
left join base-dat s on m.pid = s.pid
where b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;
You can rewrite your query as below to get the desired output
select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b
left join med_dat m on b.pid = m.pid
left join base_dat s on m.pid = s.pid
left join med_dat m1 on m.pid = m1.pid
and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
DEMO
Edit from comment, join base_dat using pid from user table
select b.pid, s.surname, s.name, m.dateLastEntry as lastEntry, m.weight, m.pulse
from users b
left join med_dat m on b.pid = m.pid
left join base_dat s on b.pid = s.pid
left join med_dat m1 on m.pid = m1.pid and m.dateLastEntry < m1.dateLastEntry
where m1.pid is null
and b.role = 'Patient'
group by b.pid, s.surname, s.name, m.weight;
Demo

MYSQL one big left outer join or 3 left outer joins same tables?

I'm not sure how to join two tables onto my main query. The two tables that determine the accountnumber join to each of the 3 main tables for different situations.
I'm trying to determine the accountnumber based on three things.
An accountnumber is based on a paycode, department and employee type which are three different tables.
Here is how they should be joined.
/* all accountnumbers have a paycode_id*/
accountcodes.paycode_id = employee_pay.id
/* all accountnumbers are assigned to either a certain department or all*/
accountcodes.department_code = department.code
/* the accountnumber can be assigned to one emp_type or both*/
accountcodes.emp_type_id = employee_infos.emp_type_id
/* the accountnumber is in table:lkp_accountcodes, that determines the account number table:accountcodes */
accountcodes.lkp_accountcodes_id = lkp_accountcodes.id
table:accountcodes
-----------------------------------------------------------------------
| ID | lkp_accountcodes_id | paycode_id | department_code | emp_type_id |
|--------------------------|------------|-----------------|-------------|
| 1 | 21 | 15 | 120 | 1 |
| 2 | 22 | 15 | 310 | 1 |
| 3 | 23 | 30 | null | 1 |
| 4 | 24 | 30 | null | 2 |
| 5 | 25 | 55 | 120 | 1 |
| 6 | 26 | 55 | 310 | 2 |
| 7 | 27 | 55 | 120 | 2 |
-----------------------------------------------------------------------
table:lkp_accountcodes
-----------------------------------
| id | company_id | accountnumber |
|-----|------------|----------------|
| 21 | 500 | 5210 |
| 22 | 500 | 6210 |
| 23 | 500 | 2211 |
| 24 | 500 | 2210 |
| 25 | 500 | 5010 |
| 26 | 500 | 6000 |
| 27 | 500 | 5090 |
-----------------------------------
I don't know if I should be doing three left joins, create temporary tables, or one big left outer join like the one below?
Also, I'm not sure how to group it and if the departent code is null then the accountnumber should be detemined by the paycode_id and emp_type_id.
Help me with the Query below.
SELECT i.employee, d.department, e1.paycode, a1.accountnumber
FROM employee_pay e1
INNER JOIN employee_infos i ON e1.emp_info_id = i.id
INNER JOIN department d ON i.department_id = d.id
LEFT OUTER JOIN accountcodes ac ON ac.paycode_id = e1.id
AND ac.emp_type_id = i.emp_type_id
AND ac.department_code = d.code -- if null?
LEFT OUTER JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id
-- group?
expected result
emp_number | emp_type | deptartment | pay_code | account_number
123 | temp | 120 | CPP Ded | 5210
456 | reg | 310 | CPP Ded | 6210
789 | temp | null | ExpReim | 2210
987 | reg | null | ExpReim | 2211
654 | reg | 145 | StatHol | 5010
321 | temp | 145 | StatHol | 5090
333 | temp | 532 | StatHol | 6000
Do the fact you have not aggregation function then if you want avoid duplicated row instead of GROUP BY you should use DISTINCT
SELECT DISTINCT i.employee, d.department, e1.paycode, a1.accountnumber
FROM employee_pay e1
INNER JOIN employee_infos i ON e1.emp_info_id = i.id
INNER JOIN department d ON i.department_id = d.id
LEFT JOIN accountcodes ac ON ac.paycode_id = e1.id
AND ac.emp_type_id = i.emp_type_id
AND ac.department_code = d.code
LEFT JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id
This should be ok.
SELECT i.employee, d.department, e1.paycode, a1.accountnumber
FROM employee_pay e1
JOIN employee_infos i ON e1.emp_info_id = i.id
JOIN department d ON i.department_id = d.id
JOIN accountcodes ac ON ac.paycode_id = e1.id
AND ac.emp_type_id = i.emp_type_id
AND ac.department_code = d.code -- if null?
LEFT OUTER JOIN lkp_accountcodes lgc on gp.lkp_gl_code_id = lgc.id;