JOIN 或任何其他附近的 PostgreSQL 语法错误
PostgreSQL Syntax Error near JOIN, or anyother
我在 PostgreSQL 数据库中导入了这个奥林匹克数据集。我想使用 select 语句创建 table 并且还有更多内容。但我收到此错误:
ERROR: column foo.age does not exist
LINE 17: (case when foo.Age=NULL then foo.Age=26 else foo.Age end...
^
HINT: Perhaps you meant to reference the column "ae.age".
SQL state: 42703
Character: 628
我打算做的是:
- 更改年龄、身高、体重列的类型
- 获取上述数据并将其与我要替换 NULL 值的另一个查询结合起来
- 使用以上两个数据创建新的 table。所有这些都一次性完成。
我对这个数据库还是个新手,我实际上是在 SQLite 中完成的,它工作得很好,但在这里它不会工作。如果您有任何其他方法可以做到这一点,请告诉我。谢谢你!
这是我的代码:
CREATE TABLE Female_participants AS
SELECT DISTINCT
ae.id AS Player_id,
ae.Name,
( /* Here what i want to do is im thinking like foo table which i joined to this table is giving me
(this and following column where i want to replace NULL values) age,weight and height as int,float,float */
CASE
WHEN/*foo.age suppose to be column from foo table which is casted to int*/
foo.Age = NULL
THEN
foo.Age = 26
ELSE
foo.Age
END
)
AS Age ,
(
CASE
WHEN/*foo.height suppose to be float*/
foo.height = NULL
THEN
FLOOR(AVG(foo.height))
ELSE
foo.height
END
)
AS Height,
(
CASE
WHEN /*foo.Weight suppose to be float*/
foo.weight = NULL
THEN
FLOOR(AVG(foo.weight))
ELSE
foo.weight
END
)
AS weight, City, Team AS Country
FROM
athlete_events ae
INNER JOIN
(
SELECT DISTINCT
id,
/*Here i want to convert the dtype from TEXT to int or float*/
CAST(age AS INT) thag,
CAST(height AS FLOAT) AS thht,
CAST(weight AS FLOAT) AS thwt
FROM
athlete_events /* i read on https://www.postgresqltutorial.com/.. that i can self-join table */
)
AS foo /*Joining this table on id*/
ON foo.id = ae.id
GROUP BY
id,
name
你的代码有两个问题:
- 关系“foo”实际上没有“年龄”列,它的列是 id,thag,thht,thwt
- 运算符“smth = NULL”不存在,必须使用 smth IS NULL 或对此处的简单情况使用 COALESCE 函数
代码可以是这样的:
CREATE TABLE Female_participants AS
SELECT DISTINCT
ae.id AS Player_id,
ae.Name,
COALESCE(foo.Age, 26) AS Age ,
COALESCE(foo.height, foo_avg.height) AS Height,
COALESCE(foo.weight, foo_avg.weight) AS weight,
City,
Team AS Country
FROM athlete_events AS ae
INNER JOIN (
SELECT DISTINCT
id,
age::INT,
height::FLOAT,
weight::FLOAT
FROM athlete_events
) AS foo ON foo.id = ae.id
INNER JOIN (
SELECT
AVG(height::FLOAT) AS height,
AVG(weight::FLOAT) AS weight
FROM athlete_events
) AS foo_avg ON TRUE;
问题取决于以下事实:当子查询 athlete_avents
时,您将 age、height 和 weight 列重命名为 thag、thht、thwt。
所以在您的 CASE 块中,您应该使用重命名的列名,而不是原来的列名。
I.E.
( /* Here what i want to do is im thinking like foo table which i joined to this table is giving me
(this and following column where i want to replace NULL values) age,weight and height as int,float,float */
CASE
WHEN/*foo.age suppose to be column from foo table which is casted to int*/
foo.thag= NULL
THEN
foo.thag = 26
ELSE
foo.thag
END
)
AS Age ,
(
CASE
WHEN/*foo.height suppose to be float*/
foo.thht= NULL
THEN
FLOOR(AVG(foo.thht))
ELSE
foo.thht
END
)
AS Height,
(
CASE
WHEN /*foo.Weight suppose to be float*/
foo.thwt = NULL
THEN
FLOOR(AVG(foo.thwt ))
ELSE
foo.thwt
END
)
AS weight
您的查询或多或少可以归结为:
SELECT DISTINCT
ae.id AS Player_id,
ae.Name,
coalesce( foo.age::int ,26 ) ::int AS Age ,
coalesce( foo.height::float , FLOOR(AVG(foo.height::float ) over())::float ) ::float AS Height ,
coalesce( foo.weight::float , FLOOR(AVG(foo.weight::float ) over())::float ) ::float AS Weight ,
City,
Team AS Country
FROM
athlete_events foo ;
尝试阅读以下 coalesce , double colon cast , and aggregate function without group by clause 的链接。
我在 PostgreSQL 数据库中导入了这个奥林匹克数据集。我想使用 select 语句创建 table 并且还有更多内容。但我收到此错误:
ERROR: column foo.age does not exist LINE 17: (case when foo.Age=NULL then foo.Age=26 else foo.Age end... ^ HINT: Perhaps you meant to reference the column "ae.age". SQL state: 42703 Character: 628
我打算做的是:
- 更改年龄、身高、体重列的类型
- 获取上述数据并将其与我要替换 NULL 值的另一个查询结合起来
- 使用以上两个数据创建新的 table。所有这些都一次性完成。 我对这个数据库还是个新手,我实际上是在 SQLite 中完成的,它工作得很好,但在这里它不会工作。如果您有任何其他方法可以做到这一点,请告诉我。谢谢你! 这是我的代码:
CREATE TABLE Female_participants AS
SELECT DISTINCT
ae.id AS Player_id,
ae.Name,
( /* Here what i want to do is im thinking like foo table which i joined to this table is giving me
(this and following column where i want to replace NULL values) age,weight and height as int,float,float */
CASE
WHEN/*foo.age suppose to be column from foo table which is casted to int*/
foo.Age = NULL
THEN
foo.Age = 26
ELSE
foo.Age
END
)
AS Age ,
(
CASE
WHEN/*foo.height suppose to be float*/
foo.height = NULL
THEN
FLOOR(AVG(foo.height))
ELSE
foo.height
END
)
AS Height,
(
CASE
WHEN /*foo.Weight suppose to be float*/
foo.weight = NULL
THEN
FLOOR(AVG(foo.weight))
ELSE
foo.weight
END
)
AS weight, City, Team AS Country
FROM
athlete_events ae
INNER JOIN
(
SELECT DISTINCT
id,
/*Here i want to convert the dtype from TEXT to int or float*/
CAST(age AS INT) thag,
CAST(height AS FLOAT) AS thht,
CAST(weight AS FLOAT) AS thwt
FROM
athlete_events /* i read on https://www.postgresqltutorial.com/.. that i can self-join table */
)
AS foo /*Joining this table on id*/
ON foo.id = ae.id
GROUP BY
id,
name
你的代码有两个问题:
- 关系“foo”实际上没有“年龄”列,它的列是 id,thag,thht,thwt
- 运算符“smth = NULL”不存在,必须使用 smth IS NULL 或对此处的简单情况使用 COALESCE 函数 代码可以是这样的:
CREATE TABLE Female_participants AS
SELECT DISTINCT
ae.id AS Player_id,
ae.Name,
COALESCE(foo.Age, 26) AS Age ,
COALESCE(foo.height, foo_avg.height) AS Height,
COALESCE(foo.weight, foo_avg.weight) AS weight,
City,
Team AS Country
FROM athlete_events AS ae
INNER JOIN (
SELECT DISTINCT
id,
age::INT,
height::FLOAT,
weight::FLOAT
FROM athlete_events
) AS foo ON foo.id = ae.id
INNER JOIN (
SELECT
AVG(height::FLOAT) AS height,
AVG(weight::FLOAT) AS weight
FROM athlete_events
) AS foo_avg ON TRUE;
问题取决于以下事实:当子查询 athlete_avents
时,您将 age、height 和 weight 列重命名为 thag、thht、thwt。
所以在您的 CASE 块中,您应该使用重命名的列名,而不是原来的列名。
I.E.
( /* Here what i want to do is im thinking like foo table which i joined to this table is giving me
(this and following column where i want to replace NULL values) age,weight and height as int,float,float */
CASE
WHEN/*foo.age suppose to be column from foo table which is casted to int*/
foo.thag= NULL
THEN
foo.thag = 26
ELSE
foo.thag
END
)
AS Age ,
(
CASE
WHEN/*foo.height suppose to be float*/
foo.thht= NULL
THEN
FLOOR(AVG(foo.thht))
ELSE
foo.thht
END
)
AS Height,
(
CASE
WHEN /*foo.Weight suppose to be float*/
foo.thwt = NULL
THEN
FLOOR(AVG(foo.thwt ))
ELSE
foo.thwt
END
)
AS weight
您的查询或多或少可以归结为:
SELECT DISTINCT
ae.id AS Player_id,
ae.Name,
coalesce( foo.age::int ,26 ) ::int AS Age ,
coalesce( foo.height::float , FLOOR(AVG(foo.height::float ) over())::float ) ::float AS Height ,
coalesce( foo.weight::float , FLOOR(AVG(foo.weight::float ) over())::float ) ::float AS Weight ,
City,
Team AS Country
FROM
athlete_events foo ;
尝试阅读以下 coalesce , double colon cast , and aggregate function without group by clause 的链接。