将选择案例的总和转换为临时变量

时间:2014-08-12 19:29:49

标签: sql-server

我试图在案例中使用select并将sum列中的所有值相加并将其存储在变量中。

declare @testmoney = 0

SELECT
   CASE
      WHEN (sum >= 500 and sum < 10) THEN 
         CAST((@testmoney + round(sum * 0.10, 0)) AS DECIMAL)
      WHEN (sum >= 1000 and sum < 20) THEN
         CAST((@testmoney+ round(sum * 0.20, 0)) AS DECIMAL)
      WHEN (sum >= 30) THEN 
         CAST((@testmoney+ round(sum * 0.30, 0)) AS DECIMAL)
      ELSE 0
   END as s, 
   sum
FROM 
...

1 个答案:

答案 0 :(得分:0)

试试..

    declare @sum money = 0

SELECT
@sum =
    sum(
    CASE
      WHEN (b.amount >= 500 and b.amount < 1000) THEN 
         CAST((@sum + round(b.amount * 0.03, 0)) AS DECIMAL (13,4))
      WHEN (b.amount >= 1000 and b.amount < 3000) THEN
         CAST((@sum + round(b.amount * 0.05, 0)) AS DECIMAL (13,4))
      WHEN (b.amount >= 3000) THEN 
         CAST((@sum + round(b.amount * 0.1, 0)) AS DECIMAL (13,4))
      ELSE 0
   END ) 
FROM 
bonus b