【问题标题】:"INSERT INTO" an output table?“插入”输出表?
【发布时间】:2016-12-08 04:00:14
【问题描述】:

我的查询如下所示:

select hour, count(*)
from table
group by hour

我希望返回看起来像:

hour  count
8     1
9     0
10    3

但它看起来像:

hour  count
8     1
10    3

这是因为 table 中没有 9 的条目。我能想到的唯一解决方案是使用围绕查询的 insert into 语句。所以,类似:

insert into (
select hour, count(*)
from table
group by hour)
values (9, 0)

但是这在 Spark SQL 中是不正确的语法。

是否可以插入到作为查询结果生成的表中而不将该表保存到数据库中?有没有其他方法可以完成我想要完成的事情?

【问题讨论】:

  • 联合怎么样。 select hour, count(*) from table group by hour union select 9 ,0
  • 返回org.apache.spark.sql.AnalysisException: missing EOF at 'select' near 'union'; line 28 pos 0
  • stackoverflow.com/a/1271845/5308100 看到这个答案。应该做我相信的伎俩。

标签: sql apache-spark-sql apache-zeppelin


【解决方案1】:

这样的东西应该适用于任何数据库引擎:

select hour, sum(records) totalRecords
from (
select 0 hour, 0 records
from table
union
select 1 hour, 0 records
from table
...
union 
select 23 hour, 0 records
from table
union
select hour, count(*) records
from table
group by hour
) derivedTable

当然,这假设该小时代表一天中的小时。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    相关资源
    最近更新 更多