4

这是我的桌子。

我需要做的是创建一个 CASE WHEN UPDATE Query 来更新

  • CurrentCredit +1 如果 1-15

  • CurrentCredit +2 如果 16-30

  • CurrentCredit x.10 如果 >=31 则向上取整

4

1 回答 1

6

看看例子

SQL 小提琴演示

有点像

UPDATE MyTable
SET CurrentCredit = 
CASE 
  WHEN CurrentCredit BETWEEN 1 AND 15
    THEN CurrentCredit + 1
  WHEN CurrentCredit BETWEEN 16 AND 30
    THEN CurrentCredit +2
  WHEN CurrentCredit >= 31
    THEN CurrentCredit * 10
  ELSE CurrentCredit
END

另外,请记住,如果 CurrentCredit 是 a FLOAT,而不是 aINT您可能想尝试类似

UPDATE MyTable
SET CurrentCredit = 
CASE 
  WHEN CurrentCredit >= 1 AND CurrentCredit < 16
    THEN CurrentCredit + 1
  WHEN CurrentCredit >= 16 AND CurrentCredit < 31
    THEN CurrentCredit +2
  WHEN CurrentCredit >= 31
    THEN CurrentCredit * 10
  ELSE CurrentCredit
END;

编辑

对于更正的请求 (>= 31 CurrentCredit + CEILING(CurrentCredit * 0.1)),请查看使用CEILING

UPDATE MyTable
SET CurrentCredit = 
CASE 
  WHEN CurrentCredit BETWEEN 1 AND 15
    THEN CurrentCredit + 1
  WHEN CurrentCredit BETWEEN 16 AND 30
    THEN CurrentCredit +2
  WHEN CurrentCredit >= 31
    THEN CurrentCredit + CEILING(CurrentCredit * 0.1)
  ELSE CurrentCredit
END;

SQL 小提琴演示

于 2012-09-29T10:27:48.730 回答