2013-07-09 50 views
0

我試圖重命名數據庫的一列的值,我有READ-ONLY訪問權限。我也想把兩個值合併成一個。我正在使用SQL CASE查詢,但它正在轉換值不正確。這是我的SQL:SQL Case語句錯誤地替換值

SELECT 
DATE(appraisal.created_time), 
appraisal_steps.title, 
Count(appraisal.appraisal_id), 
case appraisal_steps.title 
when "archive" then "A_Archived" 
when "on_sale" then "J_Selling" 
when "under_evaluation" then "F_Evaluating" 
when "evaluated" then "H_Appraisal Complete" 
when "pending" then "B_Pending" 
when "crap" then "C_Not For Lofty" 
when "waiting_internal_expert_evaluation" then "D_Unclaimed" 
when ("expert_needs_information" OR "admin_needs_information") then "E_Waiting" 
when "seller_answered_to_expert_question" then "G_Needs_Attn" 
when "ready_to_be_sold" then "I_Ready" 
else "wtf" 
end as Status 

FROM 
appraisal 
INNER JOIN appraisal_steps ON appraisal.`status` = appraisal_steps.step_id 
WHERE 
appraisal.powersale = 0 
GROUP BY 
DATE(appraisal.created_time), 
appraisal_steps.title 
ORDER BY 
appraisal_steps.title DESC, status 

結果是對於一些狀態(正確的是複數爲static :)但seller_answered_to_expert_question轉換爲「E_Waiting」,這是不正確的?。

如果我改變「When」子句的順序,不同的職位並不起作用。

我在做什麼錯?

+1

什麼SQL的味道?此外,在英文中,複數是狀態 - 我不知道拉丁文,但我們通常不會在計算器上說出它;) – Blorgbeard

+1

我會說'狀態'的複數是'狀態',但那並不是'不會影響你的SQL。如果將OR子句分爲兩個單獨的WHEN子句會發生什麼?我期待它的工作。 'ready_to_be_sold'映射到什麼地方?是否有任何東西映射到'wtf'?我認爲正在發生的事情是,appraisal_steps.title與'(「E」或「A」)作爲'appraisal_steps.title =(「E」或「A」)'進行比較,並且這是給出TRUE(所以,我預計準備出售映射到'E_waiting'和'appraisal_steps.title'中的任何無效代碼。 –

+0

猜測MySQL,我測試過的所有其他RDBMS甚至無法處理'CASE'語句中的'OR'。 –

回答

2

的問題是,格式類似於CASE field WHEN criteria THEN END一個CASE語句不通過ORAND允許多個條件,以便在WHEN行的第二個標準是不能進行比較,以您的標題字段中的值,沒有什麼是越來越時段的在這種情況下,在「G_Needs_Attn」,「I_Ready」或「wtf」中。

您可以在幾種方法解決這個問題:

分裂您OR線分爲兩個:

when "expert_needs_information" then "E_Waiting" 
when "admin_needs_information" then "E_Waiting" 

或者使用CASE聲明的格式如下:

SELECT 
    DATE(appraisal.created_time), 
    appraisal_steps.title, 
    Count(appraisal.appraisal_id), 
    case when appraisal_steps.title = "archive" then "A_Archived" 
    when appraisal_steps.title = "on_sale" then "J_Selling" 
    when appraisal_steps.title = "under_evaluation" then "F_Evaluating" 
    when appraisal_steps.title = "evaluated" then "H_Appraisal Complete" 
    when appraisal_steps.title = "pending" then "B_Pending" 
    when appraisal_steps.title = "crap" then "C_Not For Lofty" 
    when appraisal_steps.title = "waiting_internal_expert_evaluation" then "D_Unclaimed" 
    when appraisal_steps.title = "expert_needs_information" OR appraisal_steps.title = "admin_needs_information" then "E_Waiting" 
    when appraisal_steps.title = "seller_answered_to_expert_question" then "G_Needs_Attn" 
    when appraisal_steps.title = "ready_to_be_sold" then "I_Ready" 
    else "wtf" 
    end as Status  
    FROM 
    appraisal 
    INNER JOIN appraisal_steps ON appraisal.`status` = appraisal_steps.step_id 
    WHERE 
    appraisal.powersale = 0 
    GROUP BY 
    DATE(appraisal.created_time), 
    appraisal_steps.title 
    ORDER BY 
    appraisal_steps.title DESC, status 

這種格式允許要評估的多個字段的標準或同一字段的多個標準。您也可以使用when appraisal_steps.title IN ("expert_needs_information","admin_needs_information") then "E_Waiting"作爲該行。

這裏有一個演示展示瞭如何出錯的OR作爲一個包羅萬象的結束:SQL Fiddle

+0

這是我在評論中說的詳細版本。感謝您節省我打字。 –

+0

感謝你們,山羊一氧化碳和喬納森勒弗勒!這工作,我很欣賞它很多! – Mark