SQL 视图协助

发布于 2025-01-07 03:43:28 字数 1548 浏览 5 评论 0原文

我在使用这个 sql 视图时遇到了一些问题。

CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
       CASE 
         WHEN (tb.themed_party_size % 2) = 0 THEN 
           (tb.themed_party_size-2)/2
         ELSE ((tb.themed_party_size-2)/2) + 0.5
       END themed_tables
  FROM themed_booking tb;

有人可以帮我吗?我试图在视图末尾添加一列,表示 (S-2)/2 的自然数结果,其中 Sthemed_pa​​rty_size< /代码>。

当我说自然数结果时,我的意思是对以 .5 结尾的答案进行四舍五入,因此如果 S=7 答案将为 3 而不是 2.5.

当我尝试运行上面的代码时出现的错误是

Error starting at line 1 in command:
CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
  CASE WHEN (tb.themed_party_size % 2) = 0
    THEN (tb.themed_party_size-2)/2
    ELSE ((tb.themed_party_size-2)/2) + 0.5
  END themed_tables
FROM themed_booking tb
Error at Command Line:3 Column:34
Error report:
SQL Error: ORA-00911: invalid character
00911. 00000 -  "invalid character"
*Cause:    identifiers may not start with any ASCII character other than
           letters and numbers.  $#_ are also allowed after the first
           character.  Identifiers enclosed by doublequotes may contain
           any character other than a doublequote.  Alternative quotes
           (q'#...#') cannot use spaces, tabs, or carriage returns as
           delimiters.  For all other contexts, consult the SQL Language
           Reference Manual.
*Action:

如果它有影响,我正在使用连接到 Oracle 服务器的 sqldeveloper,这样我就可以使用 PL/SQL。

I'm having a little trouble with this sql view.

CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
       CASE 
         WHEN (tb.themed_party_size % 2) = 0 THEN 
           (tb.themed_party_size-2)/2
         ELSE ((tb.themed_party_size-2)/2) + 0.5
       END themed_tables
  FROM themed_booking tb;

Can anyone help me here? I'm trying to add a column to the end of the view that the natural number result of (S-2)/2 where S is the themed_party_size.

When i say natural number result i mean like round up the answers that end in .5 so if S=7 the answer would be 3 and not 2.5.

The error I get when I try and run the above code is

Error starting at line 1 in command:
CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
  CASE WHEN (tb.themed_party_size % 2) = 0
    THEN (tb.themed_party_size-2)/2
    ELSE ((tb.themed_party_size-2)/2) + 0.5
  END themed_tables
FROM themed_booking tb
Error at Command Line:3 Column:34
Error report:
SQL Error: ORA-00911: invalid character
00911. 00000 -  "invalid character"
*Cause:    identifiers may not start with any ASCII character other than
           letters and numbers.  $#_ are also allowed after the first
           character.  Identifiers enclosed by doublequotes may contain
           any character other than a doublequote.  Alternative quotes
           (q'#...#') cannot use spaces, tabs, or carriage returns as
           delimiters.  For all other contexts, consult the SQL Language
           Reference Manual.
*Action:

If it makes a difference I am using sqldeveloper connected to an oracle server so I can use PL/SQL.

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

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

发布评论

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

评论(2

稳稳的幸福 2025-01-14 03:43:28

错误消息告诉您问题是什么。
查看行:3 列:34
这是一个无效字符

CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
  CASE WHEN (tb.themed_party_size % 2) = 0
                                  ^

我怀疑您正在尝试使用模运算符。
由于您使用的是 oracle PL/SQL,因此应该使用 mod

这是参考 Oracle/PLSQL:Mod 函数

The error message is telling you what the problem is.
Look at Line:3 Column:34
It is an invalid character

CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
  CASE WHEN (tb.themed_party_size % 2) = 0
                                  ^

My suspicion is that you are trying to use the modulo operator.
Since you are using oracle PL/SQL, you should use mod

Here is a reference Oracle/PLSQL: Mod Function

五里雾 2025-01-14 03:43:28

我认为您可以使用 CEIL()ROUND() 函数进行简化:

CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
       ROUND((tb.themed_party_size-2)/2) AS themed_tables
  FROM themed_booking tb;

不确定为什么会出现该错误。也许这是 Oracle 中不可用的 % 运算符。此链接表明:PL/SQL 基础知识。不过似乎有一个 MOD() 函数。

I think you can simplify with the CEIL() or ROUND() function:

CREATE OR REPLACE VIEW view_themed_booking AS 
SELECT tb.*,         
       ROUND((tb.themed_party_size-2)/2) AS themed_tables
  FROM themed_booking tb;

Not sure why you get that error. Perhaps it's the % operator that is not available in Oracle. This links suggests so: Fundamentals of PL/SQL. There seems to be a MOD() function though.

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