开发者

Add multiple columns in select query

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, r开发者_JAVA技巧ound 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?


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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜