SQL computed column for sum of data in another table

PnP

I have two tables:

  DiskUsage Table                               StatisticsTable
Server    DiskUsage(GB)                  Total Disk Usage        xxxx
   1         10
   2         212
   3         11

I need to create the "Total Disk Usage" as a column which works out the Sum of the "DiskUsage" column. This would need to be dynamic as more servers will be added overtime to the "DiskUsage" table.

I've done some looking into this - and I believe a Computed Column would be the easiest way to achieve this, but I'm not sure how to a). reference the other tables data or b). dynamically obtain the total of that column.

Pரதீப்

Create a trigger

CREATE TRIGGER test
ON DiskUsage
after INSERT, UPDATE
AS
  BEGIN
      UPDATE StatisticsTable
      SET    TotalDiskUsage = (SELECT Sum(DiskUsage)
                               FROM   DiskUsage)
  END 

Or as Mentioned by King.code create a view instead of having a table

CREATE VIEW StatisticsTable
AS
  SELECT Sum(DiskUsage)
  FROM   DiskUsage 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

computed column using another table in sql

From Dev

Can I use a computed column in a table to fetch data from another table columns

From Dev

SQL command single column data to another table

From Dev

Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

From Dev

How to create a computed column that references another column from another table?

From Dev

How can i create Computed Column which gets data from another table

From Dev

Azure SQL Data Warehouse Computed Column Error

From Dev

Calculate sum of column in table(no sql)

From Dev

SQL Server: SUM of a column based on another column

From Dev

Selecting data from table where sum of values in a column equal to the value in another column

From Dev

SQL insert data dynamic column name from another table

From Dev

Laravel Eloquent sum of column in another table

From Dev

Excel - sum values based on a column that match another column in another table

From Dev

SQL Pivot query based on "SUM of a column divided by SUM of another column"

From Dev

SQL: Filtering on a computed column

From Dev

SQL: Filtering on a computed column

From Dev

SQL calculating sum based on another column

From Dev

SQL: Sum and groupby, criteria for groupby in another table

From Dev

Populate SQL table column from another table

From Dev

Add column to table with data from another table

From Dev

Create a table column as a sum of two columns in sql

From Dev

Sum of row and column in SQL pivot table

From Dev

Pivot SQL Table rows to 3 column with sum

From Dev

Sum of row and column in SQL pivot table

From Dev

R sum data table column with multiple if conditions

From Dev

Sum of column in data table rlang issue

From Dev

Set two values in one table based on sum of a column in another table

From Dev

Trigger to update another table with sum of a column of original table

From Dev

SQL query to sum the column data based on the same ID's in access table

Related Related

  1. 1

    computed column using another table in sql

  2. 2

    Can I use a computed column in a table to fetch data from another table columns

  3. 3

    SQL command single column data to another table

  4. 4

    Display 2 columns from one table having max count in column 3 and display computed sum of values from another table

  5. 5

    How to create a computed column that references another column from another table?

  6. 6

    How can i create Computed Column which gets data from another table

  7. 7

    Azure SQL Data Warehouse Computed Column Error

  8. 8

    Calculate sum of column in table(no sql)

  9. 9

    SQL Server: SUM of a column based on another column

  10. 10

    Selecting data from table where sum of values in a column equal to the value in another column

  11. 11

    SQL insert data dynamic column name from another table

  12. 12

    Laravel Eloquent sum of column in another table

  13. 13

    Excel - sum values based on a column that match another column in another table

  14. 14

    SQL Pivot query based on "SUM of a column divided by SUM of another column"

  15. 15

    SQL: Filtering on a computed column

  16. 16

    SQL: Filtering on a computed column

  17. 17

    SQL calculating sum based on another column

  18. 18

    SQL: Sum and groupby, criteria for groupby in another table

  19. 19

    Populate SQL table column from another table

  20. 20

    Add column to table with data from another table

  21. 21

    Create a table column as a sum of two columns in sql

  22. 22

    Sum of row and column in SQL pivot table

  23. 23

    Pivot SQL Table rows to 3 column with sum

  24. 24

    Sum of row and column in SQL pivot table

  25. 25

    R sum data table column with multiple if conditions

  26. 26

    Sum of column in data table rlang issue

  27. 27

    Set two values in one table based on sum of a column in another table

  28. 28

    Trigger to update another table with sum of a column of original table

  29. 29

    SQL query to sum the column data based on the same ID's in access table

HotTag

Archive