2016-09-21 128 views
1

首先要做的是:我能够单向获取数据。我的目的是增加我的查询结果的可读性。我正在寻找,如果有可能的话。将查询行数作为查询结果中的列查看

我有通过设备送入的表。我想获得每个小时发送的数据,这些数据由两个相同的列组成。需要将这两列分组才能确定一种设备类型。 表结构是这样的:

| 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的计数会给我每个小时发送的数据。可以通过标识符1,标识符2,日期和小时将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 column as a subquery select 回答这个问题并没有为我工作。

你有什么想法或建议?

+0

感谢您编辑我的问题:) –

回答

0

为此,您可以使用条件汇总:

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 
+1

这给了我想要的精确视图。再次感谢 :-) –