使用Presto查询Hive表时,当该列不存在数据时如何返回列的值?

时间:2018-08-24 23:40:49

标签: sql hive presto

我有一个包含以下数据的Hive表(mytable):

-----------------------------------------------------------
| date       | device    | hits     | type                |
-----------------------------------------------------------
| 2018-08-15 | device1   | 162684   | messages-total-hits |
| 2018-08-15 | device2   | 70689941 | messages-total-hits |
| 2018-08-15 | device3   | 58979363 | messages-total-hits |
| 2018-08-15 | device4   | 125021   | messages-total-hits |
| 2018-08-15 | device5   | 78750    | messages-total-hits |
| 2018-08-15 | device6   | 2595244  | messages-total-hits |
| 2018-08-16 | device1   | 73140    | activity-total-hits |
| 2018-08-16 | device4   | 19       | activity-total-hits |
| 2018-08-16 | device5   | 75572    | activity-total-hits |
| 2018-08-16 | device6   | 2024704  | activity-total-hits |
-----------------------------------------------------------

我需要获取特定期间每天每台设备的总点击量,并且正在使用以下查询来做到这一点:

SELECT
date_column,b.device,coalesce(sum(b.hits),0) as total
FROM
(SELECT
CAST(date_column AS DATE) date_column
FROM
(VALUES
   (SEQUENCE(FROM_ISO8601_DATE('2018-08-14'),
             FROM_ISO8601_DATE('2018-08-18'),
             INTERVAL '1' DAY)
   )
) AS t1(date_array)
CROSS JOIN
UNNEST(date_array) AS t2(date_column)
) as a
LEFT JOIN
(SELECT date,device,hits
FROM
mytable
WHERE
date BETWEEN date('2018-08-14') AND date('2018-08-18')
) as b
ON a.date_column = b.date
LEFT JOIN
(SELECT distinct(device) FROM mytable) as c
on b.device = c.device
WHERE
date_column BETWEEN date('2018-08-14') AND date('2018-08-18')
GROUP BY
date_column,
c.device,
b.device
ORDER BY
date_column,
device
;

此查询产生以下结果:

------------------------------------
| date_column | device  | total    |
------------------------------------
| 2018-08-14  | null    | 0        |
| 2018-08-15  | device1 | 162684   |
| 2018-08-15  | device2 | 70689941 |
| 2018-08-15  | device3 | 58979363 |
| 2018-08-15  | device4 | 125021   |
| 2018-08-15  | device5 | 78750    |
| 2018-08-15  | device6 | 2595244  |
| 2018-08-16  | device1 | 73140    |
| 2018-08-16  | device4 | 19       |
| 2018-08-16  | device5 | 75572    |
| 2018-08-16  | device6 | 2024704  |
| 2018-08-17  | null    | 0        |
------------------------------------

问题是,如果特定设备在特定日期没有数据存在,我需要显示设备名称和总计0。我不明白为什么我的查询没有产生我想要的结果,如下:

------------------------------------
| date_column | device  | total    |
------------------------------------
| 2018-08-14  | device1 | 0        |
| 2018-08-14  | device2 | 0        |
| 2018-08-14  | device3 | 0        |
| 2018-08-14  | device4 | 0        |
| 2018-08-14  | device5 | 0        |
| 2018-08-14  | device6 | 0        |
| 2018-08-15  | device1 | 162684   |
| 2018-08-15  | device2 | 70689941 |
| 2018-08-15  | device3 | 58979363 |
| 2018-08-15  | device4 | 125021   |
| 2018-08-15  | device5 | 78750    |
| 2018-08-15  | device6 | 2595244  |
| 2018-08-16  | device1 | 73140    |
| 2018-08-16  | device2 | 0        |
| 2018-08-16  | device3 | 0        |
| 2018-08-16  | device4 | 19       |
| 2018-08-16  | device5 | 75572    |
| 2018-08-16  | device6 | 2024704  |
| 2018-08-17  | device1 | 0        |
| 2018-08-17  | device2 | 0        |
| 2018-08-17  | device3 | 0        |
| 2018-08-17  | device4 | 0        |
| 2018-08-17  | device5 | 0        |
| 2018-08-17  | device6 | 0        |
------------------------------------

谁能解释为什么在给定的一天没有设备的数据时,我的查询为什么不生成总值为0的设备名称?

1 个答案:

答案 0 :(得分:0)

您必须cross join个带有日期的不同设备,然后left join个原始表。以下查询应返回预期结果。

SELECT
a.date_column,d.device,coalesce(sum(b.hits),0) as total
FROM
(SELECT
 CAST(date_column AS DATE) date_column
 FROM
 (VALUES
   (SEQUENCE(FROM_ISO8601_DATE('2018-08-14'),
             FROM_ISO8601_DATE('2018-08-18'),
             INTERVAL '1' DAY)
   )
 ) AS t1(date_array)
CROSS JOIN UNNEST(date_array) AS t2(date_column)
) as a
CROSS JOIN (SELECT distinct device FROM mytable) as d
LEFT JOIN
(SELECT date,device,hits
 FROM mytable
 WHERE date BETWEEN date('2018-08-14') AND date('2018-08-18')
) as b ON a.date_column = b.date and b.device = d.device
GROUP BY a.date_column,d.device
ORDER BY a.date_column,d.device
;