Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 Transact-SQL (2000)
 Computed Column with CASE statement

Author  Topic 

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-03-08 : 10:56:48
Can I convert the following SELECT statement into a COMPUTED COLUMN and if so how? The counta-countz columns contain integer values. If any of the values are > 0, then I want to add 1 to the total, otherwise I want to add 0.

As a SELECT statement...it works. Am just having difficult getting it to be accepted as a COMPUTE COLUMN.

SELECT
case when counta > 0 then 1 else 0 end +
case when countb > 0 then 1 else 0 end +
case when countc > 0 then 1 else 0 end +
case when countd > 0 then 1 else 0 end +
case when counte > 0 then 1 else 0 end +
case when countf > 0 then 1 else 0 end +
case when countg > 0 then 1 else 0 end +
case when counth > 0 then 1 else 0 end +
case when counti > 0 then 1 else 0 end +
case when countj > 0 then 1 else 0 end +
case when countk > 0 then 1 else 0 end +
case when countl > 0 then 1 else 0 end +
case when countm > 0 then 1 else 0 end +
case when countn > 0 then 1 else 0 end +
case when counto > 0 then 1 else 0 end +
case when countp > 0 then 1 else 0 end +
case when countq > 0 then 1 else 0 end +
case when countr > 0 then 1 else 0 end +
case when counts > 0 then 1 else 0 end +
case when countt > 0 then 1 else 0 end +
case when countu > 0 then 1 else 0 end +
case when countv > 0 then 1 else 0 end +
case when countw > 0 then 1 else 0 end +
case when countx > 0 then 1 else 0 end +
case when county > 0 then 1 else 0 end +
case when countz > 0 then 1 else 0 end
from mytablename


Basically my input data for 1 row would look like...
0 0 0 1 2 3 0 1 1 1 1 0 ....etc 0
and I should get
0 + 0 + 0 + 1 + 1 + 1 + 0 + 1 + 1 + 1 + 1 + 0 .........giving a total (if all the other numbers were 0) of 7...ie 7 non zero columns.

Hope somebody can help me.

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

7020 Posts

Posted - 2005-03-08 : 11:32:34
What is the error message you get when you attempt to create the computed column?




CODO ERGO SUM
Go to Top of Page

spirit1
Cybernetic Yak Master

11752 Posts

Posted - 2005-03-08 : 11:33:39
put it in a trigger that updates the computed column.

Go with the flow & have fun! Else fight the flow
Go to Top of Page

TimS
Posting Yak Master

198 Posts

Posted - 2005-03-08 : 11:37:56
Have you looked at the SIGN funtion?
Do you have negative numbers in input?

Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-03-08 : 12:05:12
1. All input numbers are positive.
2. Error message is "Invalid column 'counta' is specified in a constraint or computed-column definition. Counta is itself a computed-column (albeit a simpler formula).
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-03-08 : 12:30:35
I don't think a computed column can be based on another computed column


BOL:

quote:

computed_column_expression

Is an expression defining the value of a computed column. A computed column is a virtual column not physically stored in the table. It is computed from an expression using other columns in the same table. For example, a computed column can have the definition: cost AS price * qty. The expression can be a noncomputed column name, constant, function, variable, and any combination of these connected by one or more operators. The expression cannot be a subquery.

Computed columns can be used in select lists, WHERE clauses, ORDER BY clauses, or any other locations in which regular expressions can be used, with the following exceptions:

A computed column cannot be used as a DEFAULT or FOREIGN KEY constraint definition or with a NOT NULL constraint definition. However, a computed column can be used as a key column in an index or as part of any PRIMARY KEY or UNIQUE constraint, if the computed column value is defined by a deterministic expression and the data type of the result is allowed in index columns.
For example, if the table has integer columns a and b, the computed column a+b may be indexed, but computed column a+DATEPART(dd, GETDATE()) cannot be indexed because the value may change in subsequent invocations.

A computed column cannot be the target of an INSERT or UPDATE statement.


Note Each row in a table can have different values for columns involved in a computed column, therefore the computed column may not have the same value for each row.

The nullability of computed columns is determined automatically by SQL Server based on the expressions used. The result of most expressions is considered nullable even if only non-nullable columns are present because possible underflows or overflows will produce NULL results as well. Use the COLUMNPROPERTY function (AllowsNull property) to investigate the nullability of any computed column in a table. An expression expr that is nullable can be turned into a non-nullable one by specifying ISNULL(check_expression, constant) where the constant is a non-NULL value substituted for any NULL result.




Brett

8-)
Go to Top of Page

AndrewMurphy
Master Smack Fu Yak Hacker

2916 Posts

Posted - 2005-03-09 : 09:22:12
hmmmm......me thinks I'll have to investigate a view.
Thanks!
Go to Top of Page
   

- Advertisement -