views:

32

answers:

1

Scenario is like:

SomeTable { A int, B int, C int }

I need to select and add with formula: A + 25%B + 50%C. If in decimal, round upto next full digit and display an asterisk and a spcae preciding the sum. Each column needs to be rounded off before adding.

Suppose data is

10 16 12
14 15 19

Select should return: 20, 28 *.

I am thinking of using case statements but its getting very dirty with that.
Any ideas?

+1  A: 

CASE is your only way:

DECLARE @YourTable table (A int, B int, C int)
SET NOCOUNT ON
INSERT @YourTable VALUES (10, 16, 12 )
INSERT @YourTable VALUES (14, 15, 19 )
SET NOCOUNT OFF

SELECT 
    CASE
        WHEN ROUND(A+.25*B+C*.5,0)<A+.25*B+C*.5 THEN
            CONVERT(varchar(10),CONVERT(int,ROUND(A+.25*B+C*.5,0))+1)+' *' 
        ELSE CONVERT(varchar(10),CONVERT(int,ROUND(A+.25*B+C*.5,0))) 
    END
    FROM @YourTable

OUTPUT:

------------
20
28 *

(2 row(s) affected)
KM
Thanks.. neat logic.
Aseem Gautam