How to set a calculated column using a subquery

前端 未结 2 503
旧巷少年郎
旧巷少年郎 2021-01-17 05:43

I have a table to which I would like to add a calculated column. The query I want to set it to is more complex than a standard arithmetic operation and I am unsure how to se

2条回答
  •  心在旅途
    2021-01-17 06:19

    It is not possible to have a Computed Column with a Sub Query,

    A computed column is computed from an expression that can use other columns in the same table.

    So it is not possible to have A Query but you can use Expressions Like

    ColumnA-ColumnB+ColumnC
    

    Instead, you can convert it as a View and Compute The Column values there

    Like this

    CREATE VIEW MyComputedvIEW
    AS
    SELECT
      *,
      CalculatedAmount = (SELECT sum(Amount) FROM shareholder.TransactionInput T 
                        WHERE T.ShareClassLabel = Amount.ShareClassLabel
                        AND T.ValuationDate < Amount.NAVDate
                        GROUP BY T.ShareClassLabel)
    FROM YourTable
    

提交回复
热议问题