第一件事:我能够以一种方式获取数据。我的目的是提高查询结果的可读性。我正在寻找是否可能。
我有一张由设备提供的 table 。我想获取按两个相同列分组的每小时发送的数据数。需要对这两列进行分组以确定一种设备类型。 表结构如下:
| identifier-1 | identifier-2 | day | hour | data_name | data_value |
|--------------|--------------|------------|------|-----------|------------|
| type_1 | subType_4 | 2016-08-25 | 0 | Key-30 | 4342 |
|--------------|--------------|------------|------|-----------|------------|
| type_3 | subType_2 | 2016-08-25 | 0 | Key-50 | 96 |
|--------------|--------------|------------|------|-----------|------------|
| type_6 | subType_2 | 2016-08-25 | 1 | Key-44 | 324 |
|--------------|--------------|------------|------|-----------|------------|
| type_2 | subType_1 | 2016-08-25 | 1 | Key-26 | 225 |
|--------------|--------------|------------|------|-----------|------------|
我将使用一个由所有设备发送的特定 data_name,获取此 data_name 的计数将为我提供每小时发送的数据。可以按 identifier-1、identifier-2、day 和 hour 分组得到 24 行的数字。但是,它们会针对每种设备类型重复。
| identifier-1 | identifier-2 | day | hour | count |
|--------------|--------------|------------|------|-------|
| type_6 | subType_2 | 2016-08-25 | 0 | 340 |
|--------------|--------------|------------|------|-------|
| type_6 | subType_2 | 2016-08-25 | 1 | 340 |
|--------------|--------------|------------|------|-------|
|--------------|--------------|------------|------|-------|
| type_1 | subType_4 | 2016-08-25 | 0 | 32 |
|--------------|--------------|------------|------|-------|
| type_1 | subType_4 | 2016-08-25 | 1 | 30 |
|--------------|--------------|------------|------|-------|
|--------------|--------------|------------|------|-------|
|--------------|--------------|------------|------|-------|
我想这样查看结果:
| identifier-1 | identifier-2 | day | count_of_0 | count_of_1 |
|--------------|--------------|------------|------------|------------|
| type_6 | subType_2 | 2016-08-25 | 340 | 340 |
|--------------|--------------|------------|------------|------------|
| type_1 | subType_4 | 2016-08-25 | 32 | 30 |
|--------------|--------------|------------|------------|------------|
|--------------|--------------|------------|------------|------------|
在 SQL 中,可以在结果中获取子查询和列,但在 Hive 中是不可能的。我想这就是所谓的相关子查询。
Hive column as a subquery select 这个问题的答案对我不起作用。
您有什么想法或建议吗?
最佳答案
您可以使用条件聚合来做到这一点:
select identifier1, identifier2, day,
sum(case when hour = 0 then data_value else 0 end) as cnt_0,
sum(case when hour = 1 then data_value else 0 end) as cnt_1
from t
where data_name = ??
group by identifier1, identifier2, day
order by identifier1, identifier2, day
关于hadoop - 查看计数行作为查询结果中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39605989/