2014-02-21 129 views
2

我試圖在Oracle上發出一個sql請求,但是我有點問題。我有這句話翻譯,我不知道如何..你能幫我嗎?我如何翻譯它? :(如果ORSE上的ELSE條件

IF(CODE="fuu") 
THEN(SELECT bar FROM TABLE) 
ELSE(we return 0) 

THX的建議

編輯:

我的要求是這樣的

SELECT distinct 
    A, B, C, D, E, 
    SUM(F+G+H) AS "FGH", 
    SUM(I -(FGH)) AS "I", 
    CASE WHEN code='fuu' THEN bar ELSE 0 END AS AS "What I need" 
FROM 
    TABLE 
WHERE 
    A=20 
GROUP BY 
    A, B, C, D, E; 

此解決方案的作品,但i'don't有它的好答案實際上應該爲A = 20-> bar = 7598而不是0;

+2

你灣執行此操作在SQL或PL/SQL? – arunb2w

+0

我想在SQL中這樣做。 – Drupal8ForTheWin

+2

從代碼='fuu';' –

回答

3

SQL中的一個case表達式是equi價至IF語句其他語言:

CASE WHEN condition THEN value1 [ ELSE value2 ] END 

我猜你正在尋找的東西是這樣的:

SELECT distinct 
    A, B, C, D, E, 
    SUM(F+G+H) AS "FGH", 
    SUM(I -(FGH)) AS "I", 
    CASE WHEN CODE='fuu' THEN bar ELSE 0 END As "What I need" 
FROM 
    TABLE 
WHERE 
    A=20 
GROUP BY 
    A, B, C, D, E, "What I need" 
+0

Thx you @kordirko。此解決方案有效。 :) – Drupal8ForTheWin