我在HIVE上有一个类似的数据库。
+--------+------------------+---------+
| rating | date_upd | 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 |
我需要在另一个表中发送两个SELECT的结果。如何使用HiveQL将这两者合并为一?
SELECT substr('date_upd',1,10) as 'day',
count(*) cnt
FROM tbl_one
GROUP BY
substr(date_upd,1,10);
SELECT substr('date_upd',1,7) as 'month',
count(*) cnt
FROM table_name
GROUP BY
substr('date_upd',1,7);
当我这样做时,它只返回"day"值,而不返回"month"值。
SELECT
substr('date_upd',1,7) as 'month',
count(*) cnt_month,
substr('date_upd',1,10) as 'day',
count(*) cnt_day
FROM table_name
GROUP BY
substr('date_upd',1,7),
substr('date_upd',1,10);
这取决于合并对您意味着什么。如果你试图垂直附加(从标题描述中听起来(,你可以试试这个:
垂直附加:
接头:
如果您想在打印另一个报告之后立即打印一个报告,只需执行UNION
语句
SELECT substr('date_upd',1,10) as 'day',
count(*) cnt
FROM tbl_one
GROUP BY
substr(date_upd,1,10);
UNION -- this is the operator you are looking for
SELECT substr('date_upd',1,7) as 'month',
count(*) cnt
FROM tbl_one
GROUP BY
substr('date_upd',1,7);
水平追加:
加入
然而,如果您试图水平显示内容,则可能应该使用基于联接的方法(比窗口函数更简单(,该方法可能在月份联接,并将所有的monht_date和day_date列(以防您想将这些列和计数一个接一个地放在一起(。
天 | count_day | 月count_month|||
---|---|---|---|---|
2021-07-01 12:13 | 2021-07 | 45 | ||
2021-07-02 11:07 | 5 | 2021-07 | >td>45||
2021-07-05 07:22 | 3 | 2021-07 | 45