我需要以某种方式使用 CASE 语法(超出我的范围)来影响基于标准的数据库结果。
我有一堆 0.# 形式的版税(版税)
我有一个标题 ID # (title_id),我需要显示版税的新增加,以便我可以使用这些数据。
IF: they have a current royalty of 0.0 - 0.1 = 10% raise
IF: they have 0.11 - 0.15 = 20% raise
IF: royalty >= 0.16 = 20% raise
任何帮助将非常感激。
create table royalites (
title_id char(6),
lorange integer,
hirange integer,
royalty decimal(5,2));
最佳答案
实际上,您不需要使用 case
陈述:
update royalties set royalty = royalty * 1.2
where royalty >= 0.16;
update royalties set royalty = royalty * 1.2
where royalty >= 0.11 and royalty < 0.16;
update royalties set royalty = royalty * 1.1
where royalty < 0.11;
(如果您需要原子性,则在事务控制下)。如果前两个与您的问题具有相同的乘数,您可以将它们组合起来。
它的工作原理是确保您首先执行更高的值并限制
where
中哪些行受到影响。条款。如果您觉得必须使用
case
陈述:update royalties set royalty =
case when royalty >= 0.16 then royalty * 1.2
case when royalty >= 0.11 and royalty < 0.16 then royalty * 1.2
case when royalty < 0.11 then royalty * 1.1
end;
只需更改您从表中拉出的内容(而不是更改表本身)并将其与当前进行比较:
select title_id, lorange, hirange, royalty,
case when royalty >= 0.16 then royalty * 1.2
case when royalty >= 0.11 and royalty < 0.16 then royalty * 1.2
case when royalty < 0.11 then royalty * 1.1
end as new_royalty
from royalties;
关于sql - DB2 CASE 语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2691056/