hive insert overwrite table with inner sub query of count of columns 作为结果

hive insert overwrite table with inner sub query of count of columns as result

您好,我有以下来源 table "status table"

date            status    name
2017-06-22      true      1.tar
2017-06-22      true      2.tar
2017-06-22      false     3.tar
2017-06-22      true      4.tar
2017-06-22      false     5.tar
2017-06-21      false     6.tar
2017-06-21      false     6.tar
2017-06-21      false     6.tar
2017-06-21      true      6.tar

我有低于目标 table 列的预期数据

True     False     Total    Date
3        2         5        2017-06-22
1        3         4        2017-06-21

我在下面写了查询以将数据从源 table 加载到目标 table,但它说

Expression not in GROUP BY key

SET hive.exec.dynamic.partition.mode=nonstrict;
SET hive.auto.convert.join=true;
INSERT OVERWRITE TABLE destination PARTITION(date_time)
SELECT
count(status=true) AS success,
count(status=false) AS fail,
success + fail
FROM
status;

请帮我解决丢失的 link。提前致谢。

  1. Hive 不支持 SELECT 子句中的别名引用 (success + fail)
  2. COUNT 计算非 NULL 的所有内容。 FALSE 不为 NULL。
  3. Date 是保留字。使用 `Date` 或者更好,找另一个名字。
  4. 您还没有按 Date 分组。
  5. 使用动态分区时,应选择最后的分区列。

select  count (case when status = true  then 1 end) as success
       ,count (case when status = false then 1 end) as fail
       ,count (status)                              as total
       ,`date`

from    status

group by `date`

感谢您的回答,但是我发现了我的错误,我使用的是计数函数而不是求和。这是下面的代码。

SELECT
sum(case when status ='true' then 1 else 0 end),
sum(case when status ='false' then 1 else 0 end),
sum(case when status ='true' then 1 else 0 end) + sum(case when status='false' then 1 else 0 end)
from status  where date='2017-06-21';