Trying to create a trigger to update Sum Table - mysql

I am new to triggers and have an idea that may speed up my query.
I have a table called labour, which has Id (Primary Key, AI), Job_No, Date, Paying_Rate and Working_Hrs
I have a table called Sum which has Id (Primary Key, AI), Job_No and Total_Labour
So when a new line gets added to labour or updated, I want it to insert the sum into the Sum table.
So I am thinking along these lines.
When labour added Select Sum (Paying_Rate*Working_Hrs)AS Sum, then insert that into the sum table, Total_Labour as per Job_No.
Then when another new labour is added, with the same Job_No the same query runs, but it updates the Total_Labour in the Sum table.
Looking at it like a table view.
Labour
Id
Job_No
Date
Paying_Rate
Working_Hrs
Calculated AS Sum
1
1000
10.00
10
100.00
2
1000
20.00
4
80.00
Sum
Id
Job_No
Total_Labour
1
1000
180.00
Is what I am trying possible?
Thank you

Related

How to update a column by subtracting from another table?

I have two tables cart and cages, I am trying to update the 'remaining' column by checking the item_name for all entries in cart and subtracting the quantity in the corresponding tables.
[cages]
id name total remaining
-----------------------------------
1 Cage1 10 10
2 Cage2 15 15
3 Cage3 10 10
[cart]
id item_name quantity
-----------------------------------
1 Cage1 2
2 Book4 3
There are several tables in the database that would need to be checked upon purchase however, I am not sure what would be the best way of performing these queries without joining all tables. This is the query I have been attempting to use.
UPDATE cages
SET remaining = (remaining - cart.quantity
WHERE cart.item_name = cages.name);
You must join the tables before you can update
UPDATE cages
JOIN cart on cart.item_name = cages.name
SET remaining = (remaining - cart.quantity)

MYSQL Group in one table and multiply with numbers from another table

I have two tables as below:
goods_in:
in_id|pid|in_num|in_date
1 1001 10 2020-06-28
2 1002 20 2020-06-28
3 1001 20 2020-05-25
......
stock:
stock_id|pid|num|price
1 1001 10 5
2 1002 15 6
3 1003 20 7
...
The "goods_in" table stores the records that all goods come into warehouse, the pid in this table is the same as in table "stock" which is the product ref code. There will be multiple records for each product in "goods_in" table.
The "stock" table stores all the SKU we are holding and the current stock level as well as the product cost.
What I'm trying to do is:
Group by pid AND date (Year+Month) from "goods_in" so I get sub-total number of goods booked-in in each month.
Multiple sub-total with stock.price.
Get total amount of above, something like SUMPRODUCT in excel.
Exports to html table or excel.
I've tried several answers from SO with GROUP BY/ROLLUP/JOIN, and apparently I haven't made it right as expected. I can make this simple if I just add a cost column to the "goods_in" table but that would make it untidy.
I'm still on my learning curves with MYSQL, forgive me if this looks simple to you guys.
Thanks.
Ken
Hard to know for sure what you want, but it sounds something like this:
select
pid,
year(in_date) as year,
month(in_date) as month,
sum(goods_in.in_num * stock.price)
from goods_in
join stock using (pid)
group by pid, year, month
For exporting to html or excel, you are best off asking a separate question.

T-SQL query procedure-insert

I am wondering if any of you would be able to help me. I am trying to loop through table 1 (which has duplicate values of the plant codes) and based on the unique plant codes, create a new record for the two other tables. For each unique Plant code I want to create a new row in the other two tables and regarding the non unique PtypeID I link any one of the PTypeID's for all inserts it doesnt matter which I choose and for the rest of the fields like name etc. I would like to set those myself, I am just stuck on the logic of how to insert based on looping through a certain table and adding to another. So here is the data:
Table 1
PlantCode PlantID PTypeID
MEX 1 10
USA 2 11
USA 2 12
AUS 3 13
CHL 4 14
Table 2
PTypeID PtypeName PRID
123 Supplier 1
23 General 2
45 Customer 3
90 Broker 4
90 Broker 5
Table 3
PCreatedDate PRID PRName
2005-03-21 14:44:27.157 1 Classification
2005-03-29 00:00:00.000 2 Follow Up
2005-04-13 09:27:17.720 3 Step 1
2005-04-13 10:31:37.680 4 Step 2
2005-04-13 10:32:17.663 5 General Process
Any help at all would be greatly appreciated
I'm unclear on what relationship there is between Table 1 and either of the other two, so this is going to be a bit general.
First, there are two options and both require a select statement to get the unique values of PlantCode out of table1, along with one of the PTypeId's associated with it, so let's do that:
select PlantCode, min(PTypeId)
from table1
group by PlantCode;
This gets the lowest valued PTypeId associated with the PlantCode. You could use max(PTypeId) instead which gets the highest value if you wanted: for 'USA' min will give you 11 and max will give you 12.
Having selected that data you can either write some code (C#, C++, java, whatever) to read through the results row by row and insert new data into table2 and table3. I'm not going to show that, but I'll show how the do it using pure SQL.
insert into table2 (PTypeId, PTypeName, PRID)
select PTypeId, 'YourChoiceOfName', 24 -- set PRID to 24 for all
from
(
select PlantCode, min(PTypeId) as PTypeId
from table1
group by PlantCode
) x;
and follow that with a similar insert.... select... for table3.
Hope that helps.

MySQL Column that shows order of items

I'm new to MySQL procedures and triggers and constraints and I'm wondering how I can set one of my columns to be dependent on another column. For example, I have a table of products and weekly sales similar to:
Product_ID Sales Ranking
1 13
2 12
3 543
4 435
5 97
I want my ranking column to be automatically updated whenever a value in the sales is updated or a new value is inserted (this table will only hold at most 50 products), which means product IDs will come and go. How can I set it so that the Ranking is always an x value such that x represents what position it would be it the product id was ordered by sales DESC?
Product ID is NOT an AI column or Primary Index

Database design price changes for every day

I got a table (tableA) with an auto current_timestamp column.
All entries will be counted and at the moment multiplied by a constant to get the price.
Lets say the count is 1000 and the constant is 2.00. That makes a price of 1000 * 2.00 = 2$
Now i want to change this constant whenever I need to.
I probably have to create a table (tableB) with the values
date pricefactor
2014-09-01 0.002
2014-09-20 0.001
2014-10-01 0.003
When i request now the price for all of the entries in tableA i want to get the value by multiplying all entries times the pricefactor in tableB.
So i would maybe got a count of 100 till the 20th, 1000 till the 1st of october and 200 after the 1st of october.
That would make:
100 * 0.002 = 0.200$
1000 * 0.001 = 1.000$
200 * 0.003 = 0.600$
---------------------
total = 1.800$
Is this thought ok till yet? or is there something i should change?
I just want to do it right so I ask for your opinion ;)
Add one more column named count to table B. You can write a trigger for Table A. Whenever there is a change in table A (add or delete), increment/decrement the count in table B for the row containing the time-stamp. Finally multiply count*respective price from table B and aggregate everything.
The best idea I would suggest is to create an api to retrieve live price.