SQL How to sum from another table and insert in another table

前端 未结 2 753
小蘑菇
小蘑菇 2021-01-29 12:11

For now I sum in Delphi app and then insert it in SQL database
but I want database to sum and insert automatically when I insert new ExpenseA

2条回答
  •  没有蜡笔的小新
    2021-01-29 12:36

    You can create a subquery grouping the expense amount for each project, and then updating the Projects table.

    UPDATE ProjectsTable
    SET ProjectsTable.ExpenseTotal = S1.ExpenseAmount
    FROM ProjectsTable
    INNER JOIN (SELECT ProjectID, SUM(ExpenseAmount) as ExpenseAmount
    FROM ExpenseTable
    GROUP BY ProjectID) as S1
    ON ProjectsTable.ProjectID = S1.ProjectID
    

提交回复
热议问题