【问题标题】:Unknown error while creating table in SQL notebook在 SQL 笔记本中创建表时出现未知错误
【发布时间】:2020-07-24 08:08:43
【问题描述】:

我尝试在 SQL 笔记本中使用以下语法创建表,但不断收到错误消息,例如:“ Expected list-item:” 我的代码:

create table
   player_info as 
( 
select distinct id,
         name,
         sex,
         age,
         case(when height is NULL then floor(avg(height)) else height end) as Hght,
         case(when weight is NULL then floor(avg(weight)) else weight end ) as Wght,
         team as country,
         city
from athlete_events
group by id,
         name,
         sex,
         age
)

例如,我想使用主数据集中的 select 子句中提到的字段创建一个表,以便我可以 删除 NULL 值。预期的输出应该是一个表,其中年龄、身高和体重列中没有空值。

已编辑:我找到了这个问题的解决方案,它实际上是括号,它是 case 语句和 select 子句的问题,它不应该包含在括号中,它认为这是子查询的一部分。此外,考虑到我使用“NA”作为该列的 dtype 的条件是 TEXT。代码应如下所示:

create table
   player_info as 

select distinct id,
         name,
         sex,
         age,
         (case when height ='NA' then floor(avg(height)) else height end) as Hght,
         (case when weight ='NA' then floor(avg(weight)) else weight end ) as Wght,
         team as country,
         city
from athlete_events
group by id,
         name,
         sex,
         age

【问题讨论】:

  • 错误信息:'expected-list: '
  • 更新您的问题添加适当的数据样本和预期的结果..

标签: sql database sqlite data-analysis create-table


【解决方案1】:

程序在这里抱怨最外面的括号:

 case(when height is NULL then floor(avg(height)) else height end) as Hght,

下一行也需要更正。 CASE 是一个表达式,而不是一个函数。您可以在the doc 中查看正确的语法。

请注意,这不会实现既定目标。子查询基本上一次只对一个id 起作用。如果特定行中的身高或体重为 NULL,floor(avg(height)) 将为 0。

【讨论】:

  • 谢谢我学习了提高我知识的案例语法
【解决方案2】:

casewhen 之间不允许有括号。而不是:

     case(when height is NULL then floor(avg(height)) else height end) as Hght,
     case(when weight is NULL then floor(avg(weight)) else weight end ) as Wght,

用途:

     (case when height is NULL then floor(avg(height)) else height end) as Hght,
     (case when weight is NULL then floor(avg(weight)) else weight end ) as Wght,

我真诚地怀疑这是否能满足您的真正需求。 . . heightweight 不在 group by 中。事实上,我怀疑 group by 根本不需要,你真的打算:

select id, name, sex, age,
       coalesce(height, floor(avg(height) over ()),
       coalesce(weight, floor(avg(weight) over ())
       team as country, city
from athlete_events

【讨论】:

  • 谢谢。实际上,我最终创建了表而没有执行 avg 或更改 NULL 值,但后来我更新了表,这一次对我有用。
猜你喜欢
  • 1970-01-01
  • 2021-10-20
  • 1970-01-01
  • 2019-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-02-13
相关资源
最近更新 更多