DB2 CASE 语句

发布于 2024-08-30 01:20:48 字数 443 浏览 3 评论 0原文

我需要以某种方式使用 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));

I need to somehow use the CASE syntax (which is beyond me) to affect the database results based on criteria.
I have a bunch of royalties in 0.# form (royalty)
I have a title ID # (title_id) and I need to show the new increase in royalties so that I can use the data.

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

Any help would be much appreciated.

    create table royalites (
title_id    char(6),
lorange     integer,
hirange     integer,
royalty     decimal(5,2));

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒几许 2024-09-06 01:20:48

实际上,您不需要需要使用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;

Actually, you don't need to use the case statement:

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;

(under transactional control if you need atomicity). You could possibly combine the first two if they have the same multiplier as your question states.

It works by ensuring you do the higher values first and limit what rows get affected in the where clause.

If you feel you must use a case statement:

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;

To just change what you're pulling out of the table (rather than changing the table itself) and compare it with the current:

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;
请帮我爱他 2024-09-06 01:20:48

我不知道确切的 DB2 语法,也不知道它是否与 Oracle 或 SQL Server 不同,但我猜想如下:

update royalties as r
set r.royalty = r.royalty * (
    select case 
                when r.royalty between 0.0 and 0.1 then 1.1
                when r.royalty > 0.11 then 1.2
            and
        from royalties
)

如果我正确理解问题,则此代码周围的某些内容可以完成这项工作。每当更新启动时,这都会对每一行应用加薪。如果您希望对每一行执行条件更新,您可以添加一个 where 子句。

编辑

是的,但我想在不改变数据的情况下显示这一点,这样我就可以显示两个数字的比较

您的意思是您只想执行一个选择语句,其中初始值在一列中,而增加的值在另一列中?

I don't know the exact DB2 syntax, neither whether it is different from Oracle or SQL Server, but I would guess something like the following:

update royalties as r
set r.royalty = r.royalty * (
    select case 
                when r.royalty between 0.0 and 0.1 then 1.1
                when r.royalty > 0.11 then 1.2
            and
        from royalties
)

Something around this code could do the job, if I understand the question correctly. This would apply the raise for each row whenever the update is launched. You might add a where clause if you wish to perform a conditional update for each row.

EDIT

Yes, But I want to show this without altering the data, so I can show a comparison of the two numbers

Do you mean you only want to perform a select statement with the initial value in one column, and the raised value in another?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文