2012-02-19 90 views
0

我對這個sql視圖有點麻煩。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_party_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: 

如果它的確與衆不同,我使用的SQLDeveloper連接到Oracle服務器,所以我可以使用PL/SQL。

+0

哈哈哦對不起忘了加上錯誤! – geekman92 2012-02-19 02:53:11

回答

2

我想你可以用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中不可用的%運算符。這個鏈接建議如此:Fundamentals of PL/SQL。雖然似乎有一個MOD()函數。

+0

這太棒了,非常棒!謝謝! =) – geekman92 2012-02-19 03:07:50

3

錯誤消息告訴你問題是什麼。 看行: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 Function

+0

哦,好吧,這是很好的知道!謝謝! =) – geekman92 2012-02-19 03:14:47