2015-11-26 73 views
0

我對SQL很陌生,MS Access & PostgreSQL。我很快得到了這個查詢任務。我首先閱讀了基本的SQL語法,然後用邏輯編寫了這些語法。不知怎的,我得到這個SELECT錯誤:如何在SQL中獲得正確的SELECT語句?

[http://i.stack.imgur.com/bqjFy.png]

表和字段的名稱是否正確。以下是對我的目的很短的描述 enter image description here

SELECT 
    change_incoming_area AS Bodenfläche, 
    CASE 
    WHEN change_type_id = 2 
    AND (NOT cng.city_old_id=cng.city_new_id 
    AND EXTRACT(YEAR FROM cng.date_change) = EXTRACT(YEAR FROM tb_city_annual_population.population_year)) THEN 
     ROUND((cng.change_incoming_area/SELECT pop.city_area FROM tb_city_annual_population pop WHERE pop.city_id=cng.city_old_id)*100,2) 
    WHEN change_type_id = 2 
    AND (cng.city_old_id=cng.city_new_id 
    AND EXTRACT(YEAR FROM cng.date_change) = EXTRACT(YEAR FROM tb_city_annual_population.population_year) 
    AND cng.change_incoming_area<SELECT pop.city_area FROM tb_city_annual_population pop WHERE pop.city_id=cng.city_old_id) THEN 
     ROUND(((SELECT pop.city_area FROM tb_city_annual_population pop WHERE pop.city_id=cng.city_old_id AS origarea-cng.change_incoming_area)/origarea)*100,2) 
    WHEN change_type_id = 2 
    AND (cng.city_old_id=cng.city_new_id 
    AND EXTRACT(YEAR FROM cng.date_change) = EXTRACT(YEAR FROM tb_city_annual_population.population_year) 
    AND ((cng.change_incoming_area+SELECT pop.city_area FROM tb_city_annual_population pop WHERE pop.city_id=cng.city_old_id AS origarea3)/origarea3)*100>100) THEN 
     ROUND(((cng.change_incoming_area+SELECT pop.city_area FROM tb_city_annual_population pop WHERE pop.city_id=cng.city_old_id AS origarea2)/origarea2)*100,2) 
    ELSE 100 
    END 
FROM tb_change cng; 
+0

語法錯誤。 SELECT在另一個SELECT(即一個子查詢)內必須括在圓括號中。 – jarlh

+0

很難理解你想要做什麼。也許你可以做JOIN而不是所有這些子查詢? – jarlh

+0

簡化,直到它工作。然後你知道問題出在哪裏,並且可以專注於此。 –

回答

1

由於jarlh立即說:子查詢必須在括號中。

此行

ROUND((cng.change_incoming_area/select ...) * 100, 2) 

必須是

ROUND((cng.change_incoming_area/(select ...)) * 100, 2) 

或簡單地

ROUND(cng.change_incoming_area/(select ...) * 100, 2) 

至於子查詢本身:它必須導致一個值僅當然。有一個表名tb_city_annual_population我希望它有一個記錄每個城市和一年。但是在你的where子句中,你只能按城市限制:

SELECT pop.city_area 
    FROM tb_city_annual_population pop 
    WHERE pop.city_id = cng.city_old_id 

這是不是給你幾個記錄(每年一個)?也許你希望它是這樣的,而不是:

SELECT pop.city_area 
    FROM tb_city_annual_population pop 
    WHERE pop.city_id = cng.city_old_id 
    AND pop.year = EXTRACT(YEAR FROM cng.date_change) 

有多個錯誤:

ROUND(((cng.change_incoming_area+SELECT pop.city_area FROM tb_city_annual_population pop 
     WHERE pop.city_id=cng.city_old_id AS origarea2)/origarea2)*100,2) 

什麼是AS origarea2呢?你不能在表達式中使用AS。它應該是

ROUND((cng.change_incoming_area + (SELECT pop.city_area FROM tb_city_annual_population pop 
     WHERE pop.city_id = cng.city_old_id))/origarea2 * 100, 2) 

同爲AS origarea3AS origarea

然後在

AND EXTRACT(YEAR FROM cng.date_change) = 
    EXTRACT(YEAR FROM tb_city_annual_population.population_year)) THEN 

您參考tb_city_annual_population.population_year,但你不從tb_city_annual_population選擇,你選擇tb_change

然後你從population_year中提取年份。那麼population_year是約會嗎?這個名字表明這是一個代表一年的整數。

+0

我把括號括在子查詢'SELECT'語句中,但仍然得到相同的錯誤。 – Salman

+0

是的你是正確的我現在也爲子查詢添加了年份。但是我仍然得到錯誤。 – Salman

+0

我已更新我的答案以顯示更多錯誤。可能還有更多。我建議你建立你的查詢從小到大,所以你發現更容易。例如。只從一個WHEN子句開始。祝你好運。 –