Reputation: 6851
I have a table with 3 text columns on which I want to do some mathematical calculations. Table looks like below:
Date Column1 Column2 Column3
-----------------------------------------
2012-08-01 STABLE NEG STABLE
2012-08-02 NEG NEG STABLE
2012-08-03 STABLE STABLE STABLE
Want I want to achieve is
I want to know how I can achieve this using SQL? I'm currently working on MSSQL Server 2008 R2.
I hope my question is clear enough.
Upvotes: 1
Views: 720
Reputation: 15816
If those are the only choices for column values:
select ( Len( Column1 ) + Len( Column2 ) + Len( Column3 ) - 9 ) / 3 / 3.0 * 100.0 as 'Percent'
from Foo
The optimizer should handle the constant folding. They are shown for clarity.
Divine Comedy describes a place for people who write code like this.
Upvotes: 0
Reputation: 10882
So, something like this?
select
((case when col1 = 'Stable' then 1.0 else 0.0 end) +
(case when col2 = 'Stable' then 1.0 else 0.0 end) +
(case when col3 = 'Stable' then 1.0 else 0.0 end)) / 3.0
from yourtable
You can do some formatting on the output, but should be very close to what your looking for.
Upvotes: 3