我有一个我正在使用的查询在MySQL中工作正常,但在Postgres上工作有问题。我收到语法错误,我确信它有一个简单的解决方案,但无法在此处找到任何相关信息。
很明显,问题与第一种情况下的数字1有关。
SELECT *,
(
(case when 1stFarmers > 0 THEN 1 ELSE 0 END) +
(case when OldMcDonald > 0 THEN 1 ELSE 0 END) +
(case when NewMcDonald > 0 THEN 1 ELSE 0 END)
) as num_fruits
FROM (SELECT
fruit, sum(farmers),
sum(case when farmer = '1stFarmers' then harvest_count else 0 end) AS 1stFarmers,
sum(case when farmer = 'OldMcDonald' then harvest_count else 0 end) AS Old_McDonald,
sum(case when farmer = 'NewMcDonald' then harvest_count else 0 end) AS New_McDonald
FROM fruits
GROUP BY
farmer) ft
我回来的错误:
syntax error at or near "stFarmers"
Position: 27
(
(case when 1stFarmers > 0 THEN 1 ELSE 0 END) +
^
(case when OldMcDonald > 0 THEN 1 ELSE 0 END) +
答案 0 :(得分:2)
使用双引号"
来转义包含数字的标识符(在您的情况下,列名称):
(case when "1stFarmers" > 0 THEN 1 ELSE 0 END) +