即使特定日期表中没有数据,如何使用 impala-shell 获取日期范围的值

问题描述 投票:0回答:1

我想获取某个日期范围的数据,在这个日期范围内,某些日期在表中没有任何日期。查询结果没有显示这些日期的任何内容,如下所示,没有从 2023-07-14 开始的日期的数据。

查询

select count(*),to_date(date_column) as cdate  from table where  to_date(date_column) > '2023-07-11' group by 2 order by 2;

+----------+----------------------------+
| count(*) | cdate                      |
+----------+----------------------------+
| 413      | 2023-07-12                 |
| 316      | 2023-07-13                 |
| 400      | 2023-08-11                 |
| 302      | 2023-08-12                 |
| 325      | 2023-08-13                 |
| 13       | 2023-08-14                 |
| 767      | 2023-08-15                 |
+----------+----------------------------+

我想得到低于预期格式的结果。

+----------+----------------------------+
| count(*) | cdate                      |
+----------+----------------------------+
| 413      | 2023-07-12                 |
| 316      | 2023-07-13                 |
| 0        | 2023-07-14                 |
| 0        | 2023-07-15                 |
| 0        | 2023-07-16                 |
| 0        | 2023-07-17                 |
| 0        | 2023-07-18                 |
+----------+----------------------------+

如果某个日期没有数据,则返回 0 作为计数值。

hive impala
1个回答
0
投票

请使用

sum()
来满足该条件 -

select 
sum(case when to_date(date_column) > '2023-07-11' then 1 else 0 end ) as cnt,
to_date(date_column) as cdate  
from table where  group by 2 
order by 2;
© www.soinside.com 2019 - 2024. All rights reserved.