Mathematical Function

前端 未结 1 1444
不知归路
不知归路 2021-01-24 02:25

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

相关标签:
1条回答
  • 2021-01-24 03:22

    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;
    
    0 讨论(0)
提交回复
热议问题