• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: How to subtract two columns from different tables and set result for the first table

Resolved: How to subtract two columns from different tables and set result for the first table

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

I’m new to programming and I’m doing a project for myself to consolidate the material. I tried all the methods, but without success. How to correctly write a trigger that, after entering data in the second table in the MOVING_QUANTITY attribute, automatically subtracted SP_PRODUCT_QUANTITY from the first table from MOVING_QUANTITY in the second. And the new result was already recorded in SP_PRODUCT_QUANTITY. In other words, a replacement for the first. I wanted to implement an idea where, for example, I transfer a certain amount of goods and enter the quantity I need, and he took the quantity of a certain product from the warehouse table. Below I have given the tables, and my own trigger, but it does not work correctly as I wanted.
I’ve been sitting here for days, but I can’t think of anything.
The result I want to achieve:
FK_SP_STORAGE_ID FK_SP_PRODUCT_ID FK_SP_PRODUCT_ID
Storage-1 Coco-cola 500
Storage-1 Fanta 500

MOVING_PRODUCT MOVING_QUANTITY
Coco-cola 400
Fanta 400

Result:
FK_SP_STORAGE_ID FK_SP_PRODUCT_ID FK_SP_PRODUCT_ID
Storage-1 Coco-cola 100
Storage-1 Fanta 100

Answer:

You are referencing the wrong column name in the trigger.
Your join condition is wrong. You are not joining to the column in Moving_list at all
You are subtracting SP_PRODUCT_QUANTITY with all MOVING_QUANTITY from table Moving_list. It will result in error when the Moving_list table contains more than 1 row
The sub query should be group by MOVING_PRODUCT not MOVING_ID
The corrected trigger :

If you have better answer, please add a comment about this, thank you!

sql sql-server
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Using AWK to count multiple patterns in a file

01/04/2023

Resolved: significance letter above bar graphic in wrong order

01/04/2023

Resolved: VBA – Applying border around the areas with value/text

01/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.