" INSERT INTO"输出表?

时间:2016-08-02 17:02:46

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

我有一个如下所示的查询:

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中的正确语法。

是否可以在不将表保存到数据库的情况下插入到由查询生成的表中?有没有其他方法可以实现我想要完成的任务?

1 个答案:

答案 0 :(得分:0)

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

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

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

相关问题