首页 > 解决方案 > HIVEQL,每天记录的数量

问题描述

我在 hive 中有一个数据库,它的结构如下:

+--------+------------------+---------+
| rating |      date        | version |
+--------+------------------+---------+
| 3      | 2021-07-01 12:13 | 2.1.9   |
| 5      | 2021-07-01 10:39 | 2.2.6   |
| 4      | 2021-07-02 10:24 | 2.2.7   |
| 5      | 2021-07-02 05:37 | 3.2.4   |
| 1      | 2021-07-02 21:40 | 3.2.5   |

如何使用 HiveQL 获取每天和每月的记录数?

标签: hivehiveql

解决方案


每天计算:

select substr(`date`,1,10) as `day`,
       count(*) cnt 
  from table_name 
 group by substr(`date`,1,10);

每月:

select substr(`date`,1,7) as `month`,
       count(*) cnt 
  from table_name 
 group by substr(`date`,1,7); 

推荐阅读