postgresql语法错误,靠近join或任何其他

lstz6jyr  于 2021-07-29  发布在  Java
关注(0)|答案(3)|浏览(598)

我在postgresql数据库中导入了这个奥运会数据集。我想用select语句创建表,还有一些。但我得到了一个错误:
错误:foo.age列不存在第17行:(case when foo.age=null then foo.age=26 else foo.age end…^提示:也许你是想引用“ae.age”列。sql状态:42703字符:628
我要做的是:
更改列的类型年龄、身高、体重
获取上面的数据并将其与另一个查询连接起来,我希望在其中替换null值
使用以上两个数据创建新表。一气呵成。我还是这个数据库的新手,我实际上是在sqlite中做的,它工作得很好,但在这里它不会工作。如果你有别的办法,请告诉我。谢谢您!这是我的密码:

  1. CREATE TABLE Female_participants AS
  2. SELECT DISTINCT
  3. ae.id AS Player_id,
  4. ae.Name,
  5. ( /* Here what i want to do is im thinking like foo table which i joined to this table is giving me
  6. (this and following column where i want to replace NULL values) age,weight and height as int,float,float */
  7. CASE
  8. WHEN/*foo.age suppose to be column from foo table which is casted to int*/
  9. foo.Age = NULL
  10. THEN
  11. foo.Age = 26
  12. ELSE
  13. foo.Age
  14. END
  15. )
  16. AS Age ,
  17. (
  18. CASE
  19. WHEN/*foo.height suppose to be float*/
  20. foo.height = NULL
  21. THEN
  22. FLOOR(AVG(foo.height))
  23. ELSE
  24. foo.height
  25. END
  26. )
  27. AS Height,
  28. (
  29. CASE
  30. WHEN /*foo.Weight suppose to be float*/
  31. foo.weight = NULL
  32. THEN
  33. FLOOR(AVG(foo.weight))
  34. ELSE
  35. foo.weight
  36. END
  37. )
  38. AS weight, City, Team AS Country
  39. FROM
  40. athlete_events ae
  41. INNER JOIN
  42. (
  43. SELECT DISTINCT
  44. id,
  45. /*Here i want to convert the dtype from TEXT to int or float*/
  46. CAST(age AS INT) thag,
  47. CAST(height AS FLOAT) AS thht,
  48. CAST(weight AS FLOAT) AS thwt
  49. FROM
  50. athlete_events /* i read on https://www.postgresqltutorial.com/.. that i can self-join table */
  51. )
  52. AS foo /*Joining this table on id*/
  53. ON foo.id = ae.id
  54. GROUP BY
  55. id,
  56. name
2w3kk1z5

2w3kk1z51#

您的查询或多或少可以归结为:

  1. SELECT DISTINCT
  2. ae.id AS Player_id,
  3. ae.Name,
  4. coalesce( foo.age::int ,26 ) ::int AS Age ,
  5. coalesce( foo.height::float , FLOOR(AVG(foo.height::float ) over())::float ) ::float AS Height ,
  6. coalesce( foo.weight::float , FLOOR(AVG(foo.weight::float ) over())::float ) ::float AS Weight ,
  7. City,
  8. Team AS Country
  9. FROM
  10. athlete_events foo ;

请尝试阅读以下有关coalesce、双冒号cast和aggregate函数(不带groupby子句)的链接。

l3zydbqr

l3zydbqr2#

代码中有两个问题:
关系“foo”实际上没有列“age”,它的列是id、thag、thht、thwt
运算符“smth=null”不存在,必须使用smth is null或使用coalesce函数对于像这里这样的简单情况代码可以是这样的:

  1. CREATE TABLE Female_participants AS
  2. SELECT DISTINCT
  3. ae.id AS Player_id,
  4. ae.Name,
  5. COALESCE(foo.Age, 26) AS Age ,
  6. COALESCE(foo.height, foo_avg.height) AS Height,
  7. COALESCE(foo.weight, foo_avg.weight) AS weight,
  8. City,
  9. Team AS Country
  10. FROM athlete_events AS ae
  11. INNER JOIN (
  12. SELECT DISTINCT
  13. id,
  14. age::INT,
  15. height::FLOAT,
  16. weight::FLOAT
  17. FROM athlete_events
  18. ) AS foo ON foo.id = ae.id
  19. INNER JOIN (
  20. SELECT
  21. AVG(height::FLOAT) AS height,
  22. AVG(weight::FLOAT) AS weight
  23. FROM athlete_events
  24. ) AS foo_avg ON TRUE;
展开查看全部
9bfwbjaz

9bfwbjaz3#

这个问题取决于这样一个事实:当子查询 thlete_avents 将列age、height和weight重命名为thag、thht、thwt。
因此,在您的示例块中,应该使用重命名的列名,而不是原始列名。

  1. ( /* Here what i want to do is im thinking like foo table which i joined to this table is giving me
  2. (this and following column where i want to replace NULL values) age,weight and height as int,float,float */
  3. CASE
  4. WHEN/*foo.age suppose to be column from foo table which is casted to int*/
  5. foo.thag= NULL
  6. THEN
  7. foo.thag = 26
  8. ELSE
  9. foo.thag
  10. END
  11. )
  12. AS Age ,
  13. (
  14. CASE
  15. WHEN/*foo.height suppose to be float*/
  16. foo.thht= NULL
  17. THEN
  18. FLOOR(AVG(foo.thht))
  19. ELSE
  20. foo.thht
  21. END
  22. )
  23. AS Height,
  24. (
  25. CASE
  26. WHEN /*foo.Weight suppose to be float*/
  27. foo.thwt = NULL
  28. THEN
  29. FLOOR(AVG(foo.thwt ))
  30. ELSE
  31. foo.thwt
  32. END
  33. )
  34. AS weight
展开查看全部

相关问题