I have Database inside it 3 tables: A, B, C
A : (Item , Qty, Loc); B: (Item , Qty,Loc); C:(Item,Loc1,Loc2,Loc3,Loc4.......,Loc16) ;
I need a function for au
I would propose you, better than creating Triggers that are much more difficult to follow-up, create a View that will provide the requested information. Be aware that I put part of the query, need to be write the same portion of code for fields from 3 to 15:
CREATE VIEW C_View
AS
SELECT C.Item,
C.Loc1,
COALESCE((SELECT SUM(Qty) FROM A WHERE A.Item = C.Item AND A.Loc = C.Loc1), 0) -
COALESCE((SELECT SUM(Qty) FROM B WHERE B.Item = C.Item AND B.Loc = C.Loc1), 0)
AS Qty1,
C.Loc2,
COALESCE((SELECT SUM(Qty) FROM A WHERE A.Item = C.Item AND A.Loc = C.Loc2), 0) -
COALESCE((SELECT SUM(Qty) FROM B WHERE B.Item = C.Item AND B.Loc = C.Loc2), 0)
AS Qty2,
...
C.Loc16,
COALESCE((SELECT SUM(Qty) FROM A WHERE A.Item = C.Item AND A.Loc = C.Loc16), 0) -
COALESCE((SELECT SUM(Qty) FROM B WHERE B.Item = C.Item AND B.Loc = C.Loc16), 0)
AS Qty16,
FROM C;