我對SQL很陌生,MS Access & PostgreSQL。我很快得到了這個查詢任務。我首先閱讀了基本的SQL語法,然後用邏輯編寫了這些語法。不知怎的,我得到這個SELECT
錯誤:如何在SQL中獲得正確的SELECT語句?
[]
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;
語法錯誤。 SELECT在另一個SELECT(即一個子查詢)內必須括在圓括號中。 – jarlh
很難理解你想要做什麼。也許你可以做JOIN而不是所有這些子查詢? – jarlh
簡化,直到它工作。然後你知道問題出在哪裏,並且可以專注於此。 –