-1

I am wanting to alter my table to have a column that is the SUM() of two other fields. I have this syntax

ALTER TABLE SalesData
ADD AC AS SUM(COALESCE(val1,0)+COALESCE(val2,0))

However it produces an error of

An aggregate may not appear in a computed column expression or check constraint.

What is the proper way to add a field and set the fields value to a calculation?

1
  • You cannot use a SUM aggregation on a computer column because the computed column acts on each and every row. Aggregations use multiple rows to perform an operation, so when you think about it, aggregating doesn't make much sense at the "row" level. Hope it helps.
    – Nestor
    Apr 29, 2017 at 11:56

1 Answer 1

3

I'd not use SUM() for this. Just code

ALTER TABLE .. ADD .. AS (col1 + col2);

(see also: https://docs.microsoft.com/en-us/sql/relational-databases/tables/specify-computed-columns-in-a-table )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.