Mysql Database: Joins sums and group by - mysql

I have two tables OrderUpdate and DailyProduction
ORDER_UPDATE
itemid packages ordercode
1 10 ORANGE
2 20 ORANGE
3 40 ORANGE
1 25 APPLE
2 50 APPLE
3 100 APPLE
DAILY_PRODUCTION
date itemid packages ordercode
28-Sep 1 5 ORANGE
20-Sep 1 2 ORANGE
1-Sep 3 4 ORANGE
1-Sep 1 5 APPLE
15-Sep 2 5 APPLE
30-Sep 3 1 APPLE
And I want following as a result
itemid packages ordercode ready remaining
1 10 ORANGE 7 3
2 20 ORANGE 0 20
3 40 ORANGE 4 36
1 25 APPLE 5 20
2 50 APPLE 5 45
3 100 APPLE 1 99

If I follow you correctly, you can left join table order_update with an aggregate query that sums the packages for each (itemid, ordercode) in table daily_production:
select ou.*, coalesce(dp.ready, 0), ou.packages - coalesce(dp.ready, 0) reamining
from order_update ou
left join (
select itemid, ordercode, sum(packages) ready
from daily_production
group by itemid, ordercode
) dp on dp.itemid = ou.itemid and dp.ordercode = ou.ordercode

Related

Mysql Select Product attribute price

Sorry for my bad english)
Need help with mysql query.
There is a products table, which stores the base price for products that do not have attributes that the price depends on.
Table: product
product_id
product_name
price
1
T-shirt_1
10
2
T-shirt_2
12
10
T-shirt_10
18
15
Hat
10
There is a table of attribute values by which products are filtered.
Table: eav_ent
ent_id
product_id
attr_id
attr_value_id
1
1
1
2
2
1
1
4
3
1
1
19
4
1
2
25
5
1
2
34
6
2
2
23
7
2
3
56
Table: eav_ent_product_price
ent_product_price_id
product_id
price_name
price
1
1
T-shirt_1 size S
10
2
1
T-shirt_1 size M
15
3
1
T-shirt_1 size M with a zipper
20
4
2
T-shirt_2 size M
12
5
2
T-shirt_2 size M with a zipper
25
Table: eav_ent_product_price_attr
ent_product_price_attr_id
product_id
attr_value_id
ent_product_price_id
1
1
2
1
2
1
19
2
3
1
4
3
4
1
19
3
5
2
19
4
6
2
4
5
7
2
19
5
For each product, you can separately create prices depending on the combination of attributes, for example:
T-shirt_1 size S - 10 $
T-shirt_1 size M - 15 $
T-shirt_1 size M with a zipper - 20 $
Hat - $ 10 (base price)
How can I correctly filter products and display prices depending on the selected attributes, if they exist, or just display the base price?
What I tried:
SELECT a.* FROM(
SELECT p.* FROM product p
JOIN eav_ent en1 ON en1.product_id=p.product_id AND en1.attr_value_id=19
JOIN eav_ent en2 ON en2.product_id=p.product_id AND en2.attr_value_id=4
LEFT JOIN eav_ent_product_price ppi ... I don't know
WHERE 1 GROUP BY p.product_id
) a WHERE 1

Limit up to 10 ids from table 1 and get all the records from table 2 which matches the ids of table 1

I have a table1 for example:
orderID userId orderName orderTime...
1 11
2. 12
3. 11
4. 14
5. 11
6. 13
7. 11
8. 15
9. 16
10. 11
... ...
I have another table table2:
table2ID orderID item price ....
101 1 Apple 1.99
102 1 Banana 2.99
103 1 Grapes 0.99
104 4 pizza 6.99
105 4 drink 0.99
105. 3 chicken 1.99
106. 3 apple 1.99
I have tried this :
SELECT a.*, b.* FROM `table1` a
RIGHT JOIN table2 b on a.orderID = b.orderID
WHERE a.userID = 11 order by a.`orderTime` DESC LIMIT 25;
I want to get upto 10 unique orderIDs from table 1 of user 11 and all the details of that 10 ids from table 2. If I do LIMIT 25 then I don't get all the information.
I want my output as:
orderID userId orderName orderTime... table2ID orderID item price
1 11 101 1 Apple 1.99
1 11 102 1 Banana 2.99
1 11 103 1 Grapes 0.99
3 11 105 3 chicken 1.99
3 11 106 3 apple 1.99
SELECT a.*, b.*
FROM ( SELECT *
FROM a
WHERE a.userID = 11
LIMIT 10) as a
JOIN b
ON a.orderID = b.orderID

How to get total cost of product in mysql from following sql tables?

How Do i Calculate total cost of product from the following tables?
Table 1: components
pk_comp_id comp_name fk_comp_type_id com_price
1 Vertical Glass Panels 3 50.00
2 Side Frame 2 32.00
3 Front Frame 2 35.00
4 Roof Section 3 50.00
5 Door Frame 1 100.00
6 Standard Timber Plank 2 100.00
7 Brackets 6 20.00
8 Door Section 2 50.00
Table 2: products
pk_prod_id prod_name fk_prod_type_id
1 Small Green House 1
2 Large Green House 1
3 Small Shed 2
4 Small Summer House 3
5 Large Summer House 3
And Table 3: products_components
fk_prod_id fk_comp_id number
1 1 20
1 2 2
1 3 2
1 4 2
1 5 1
2 1 40
2 2 4
2 3 2
2 4 4
2 5 1
3 6 35
3 7 60
3 8 1
HERE is what i did so far
SELECT
SUM(`components`.`com_price`*`products_components`.`number`) AS com_g_total,
`products`.`prod_name`, `products`.`pk_prod_id`
FROM `products`
INNER JOIN `products_components` ON `products_components`.`fk_prod_id` = `products`.`pk_prod_id`
INNER JOIN `components` ON `components`.`pk_comp_id` = `products_components`.`fk_comp_id`
But this gives only one row where as i was suppose to get three row with total cost of each for pk_prod_id 1,2,3.
Add a GROUP BY clause:
SELECT
SUM(`components`.`com_price`*`products_components`.`number`) AS com_g_total,
`products`.`prod_name`, `products`.`pk_prod_id`
FROM `products`
INNER JOIN `products_components` ON `products_components`.`fk_prod_id` = `products`.`pk_prod_id`
INNER JOIN `components` ON `components`.`pk_comp_id` = `products_components`.`fk_comp_id`
GROUP BY `products`.`pk_prod_id`
Result:
COM_G_TOTAL PROD_NAME PK_PROD_ID
1334 Small Green House 1
2498 Large Green House 2
4750 Small Shed 3
See result in SQL Fiddle.

Mysql query to get the row with condition that the reduction between 2 field from different table is more than 0

I have a problem for mysql query
I have 3 table:
Table 1 : product
No_produk Code Any else field
10 6557
11 6558
12 6559
13 G18X
Table 2 : stock
Id_stock No_produk Color Size Stock
1 10 Black S 1
2 10 Black M 2
3 10 Black L 2
4 10 Black XL 1
5 11 White S 1
6 11 White M 2
7 11 White L 2
8 11 White XL 1
9 12 Blue S 1
10 12 Blue M 2
11 12 Blue L 2
12 12 Blue XL 1
13 13 White S 1
14 13 White M 2
15 13 White L 2
16 13 White XL 1
Table 3 : transaction
Id_transaction Id_stock Qty
1 2 1
2 2 1
3 5 1
4 3 1
5 1 1
6 6 1
7 4 1
8 3 1
I just want to get code, and any else field from table product where
stock at table stock substract( - ) sum(qty) at table transaction where stock.id_stock = transaction.id_stock,
and at least have one or more row of subtraction result is > 0.
Can someone help, what query should I make to get that?
thanks
Sub query to get the used stock and join against that:-
SELECT a.Code, a.`Any else`
FROM product a
INNER JOIN stock b
ON a.No_produk = b.No_produk
LEFT OUTER JOIN (SELECT Id_stock, SUM(Qty) AS SumQty FROM transaction GROUP BY Id_stock) c
ON b.Id_stock = c.Id_stock
WHERE b.stock - IFNULL(c.SumQty, 0) > 0

Select min/max from multiple items

I'll try to explain it as simple as possible:
First some database structure with dummy data.
Structure
tb_spec_fk
feature value
-----------------
1 1
1 2
1 3
1 4
1 5
2 2
2 3
3 1
3 4
4 2
4 3
4 4
5 1
5 3
5 5
6 3
6 5
tb_spec_feature
feature_id filter
------------------
1 2
2 2
3 2
4 2
5 1
6 0
tb_spec_value
value_id name
----------------
1 10
2 20
3 30
4 40
5 50
Now, what I want is the follow result
Result
feature_id min_value max_value
---------------------------------
1 10 50
2 20 30
3 10 40
4 20 40
But how?
Logic
Get from the tb_spec_feature where "filter" equals 2 the highest and lowest values which are present in the tb_spec_value table and connected together trough the tb_spec_fk table.
My attemps
A lot! But I'll spare you :)
SELECT
f.feature_id AS feature_id,
MAX(value.name) AS max_value,
MIN(value.name) AS min_value
FROM tb_spec_feature AS f
JOIN tb_spec_fk AS fk ON f.feature_id=fk.feature
JOIN tb_spec_value AS value ON fk.value=value.id
WHERE f.filter=2
GROUP BY f.feature_id
The two JOIN statements "link" the a feature to a value. GROUP BY groups all rows with the same feature id, and then you can take the min or max or any other aggregate function on those columns.
Demo
Here is how you can do it
select
tsf.feature_id,
tsvl.name as Min_Value,
tsvr.name as Max_Value
from tb_spec_feature as tsf
inner join (select feature , MIN(value) MinV,MAX(value)MaxV from tb_spec_fk group by feature order by feature)as tsfkl on tsfkl.feature = tsf.feature_id
left join tb_spec_value as tsvl on tsvl.value_id = tsfkl.MinV
left join tb_spec_value as tsvr on tsvr.value_id = tsfkl.MaxV
where tsf.filter = 2
group by tsf.feature_id
Output
feature_id | Min_Value | Max_Value
---------------------------------
1 | 10 | 50
2 | 20 | 30
3 | 10 | 40
4 | 20 | 40
Fiddle Demo