在列列表(配置单元)中放置子查询

dwbf0jvd  于 2021-07-15  发布在  Hadoop
关注(0)|答案(1)|浏览(401)

我的db-hadoop,hive我试图在select上运行一个带有子查询的查询,但没有运行。请查找下面的查询
谢谢你的帮助

select 
to_date(timestamp) as date,
count(*),
(select count(*) from table1 where to_date(timestamp)=current_date() and arrivaltime IS null) as count_null
FROM table1
WHERE to_date(ingest_timestamp) =current_date()
GROUP BY date, count_null
uidvcgyl

uidvcgyl1#

将子查询转换为案例。你也不能分组 count_null 列。如果你真的需要分组 count_null 然后在此查询之上构建一个查询。下面sql将返回 total_count , count_null ,和 count_not_null ```
select
to_date(timestamp) as date,
count(*) total_count,
SUM(CASE WHEN arrivaltime IS null then 1 else 0 END) as count_null,
SUM(CASE WHEN arrivaltime IS null then 0 else 1 END) as count_not_null,
SUM(CASE WHEN arrivaltime IS null then 0 else 1 END) - SUM(CASE WHEN arrivaltime IS null then 1 else 0 END)
as diff_not_null_and_null
FROM
table1
WHERE to_date(timestamp)=current_date
GROUP BY to_date(timestamp)

示例-假设您有这样的数据-

Flightid, timestamp, arrivaltime
Flight1, 2021-01-29,2021-01-29 10:00:00
Flight2, 2021-01-29,
Flight4, 2021-01-29,2021-01-29 11:00:00
Flight5, 2021-01-29,2021-01-29 10:30:00
Flight9, 2021-01-29,

结果将是-

timestamp, total_count, count_null, count_not_null, diff_not_null_and_null
2021-01-29, 5, 2, 3, 1

在上面的sql中,我计算了三个属性-
sum(如果arrivaltime为null,则为1,否则为0 end)-如果arrivaltime为null,则将为该行赋值1。现在,如果将它们相加,则将得到到达时间为空时的总计数。在上面的例子中,它是2。
sum(如果arrivaltime为null,则为0,否则为1结束)-与上述逻辑相反,如果arrivaltime不为null,则为1赋值。然后sum将导致所有非空行的计数。在上面的例子中,它是3。
然后是2和1之间的差值。在上面的例子中,它的3-2=1。

相关问题