DB2 CASE 语句
我需要以某种方式使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您不需要需要使用
case
语句:(如果您需要原子性,则在事务控制下)。如果前两者具有与您的问题所述相同的乘数,您可以将它们结合起来。
它的工作原理是确保您首先执行较高的值并限制
where
子句中受影响的行。如果您觉得必须使用
case
语句:只更改您从表中提取的内容(而不是更改表本身)并将其与当前的进行比较:
Actually, you don't need to use the
case
statement:(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:To just change what you're pulling out of the table (rather than changing the table itself) and compare it with the current:
我不知道确切的 DB2 语法,也不知道它是否与 Oracle 或 SQL Server 不同,但我猜想如下:
如果我正确理解问题,则此代码周围的某些内容可以完成这项工作。每当更新启动时,这都会对每一行应用加薪。如果您希望对每一行执行条件更新,您可以添加一个 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:
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
Do you mean you only want to perform a select statement with the initial value in one column, and the raised value in another?