我在一张桌子上放了几辆车,以及它们出来的那一年,我试着计算出属于以下四个年龄组的汽车的百分比: 0-4,5-9,10-19,20+。
我试着用三种不同的方式做这件事:
1.
Select (Select(Count(*) from public."Vehicles".first_year where first_year between 2018 and 2021)* 100 / (Select Count(*) From public."Vehicles")) as Percentage
From public."Vehicles";
我发现了一个错误:
ERROR: syntax error at or near "from"
LINE 1: Select (Select(Count(*) from public."Vehicles".first_year wh...
SELECT SUM(CASE WHEN public."Vehicles".first_year > 2017 AND public."Vehicles".first_year < 2022 THEN 1 ELSE 0 END) AS ['4 and under'],`
SUM(CASE WHEN public."Vehicles".first_year > 2012 AND public."Vehicles".first_year < 2018 THEN 1 ELSE 0 END) AS ['5-9'],
SUM(CASE WHEN public."Vehicles".first_year > 2002 AND public."Vehicles".first_year < 2013 THEN 1 ELSE 0 END) AS ['10-19'],
SUM(CASE WHEN public."Vehicles".first_year < 2003 THEN 1 ELSE 0 END) AS ['20 and older']
FROM public."Vehicles";
我发现了一个错误:
ERROR: syntax error at or near "["
LINE 1: ...Vehicles".first_year < 2022 THEN 1 ELSE 0 END) AS ['4 and un...
ROUND(CAST(((Count(*) from public."Vehicles".first_year where first_year between 2018 and 2021) * 100.0 / (Select Count(*) From public."Vehicles")) AS FLOAT), 2) AS Percentage;
我发现了一个错误:
ERROR: syntax error at or near "["
LINE 1: ...Vehicles".first_year < 2022 THEN 1 ELSE 0 END) AS ['4 and un...
对我能做什么有什么想法吗?我用的是pgadmin4和PostgreSQL 13
发布于 2021-06-23 16:40:24
给你的专栏更合理的名字!然后进行计算:
SELECT AVG(CASE WHEN v.first_year > 2017 AND v.first_year < 2022 THEN 1.0 ELSE 0 END) AS avg_4_and_under,
AVG(CASE WHEN v.first_year > 2012 AND v.first_year < 2018 THEN 1.0 ELSE 0 END) AS avg_5_9,
AVG(CASE WHEN v.first_year > 2002 AND v.first_year < 2013 THEN 1.0 ELSE 0 END) AS avg_10_19,
AVG(CASE WHEN v.first_year < 2003 THEN 1.0 ELSE 0 END) AS avg_20_and_older
FROM public."Vehicles" v;
请注意对查询的更改:
AVG()
来获取比率,而不是对计数使用SUM()
。这就是你想要的问题。v
.https://stackoverflow.com/questions/68103878
复制相似问题