Hive CTE,我可以查询值作为表吗?

时间:2017-04-06 00:20:42

标签: sql-server hadoop hive common-table-expression

我正在尝试创建我想稍后在数据提取中使用的查询内维度表。在MS SQL中,我可以使用这个CTE,它将使用值作为表:

with tbl_test_values as (select * from (
    VALUES  
        ('Number','1','One'),
        ('Number','5','Five'),
        ('Letter','A','First Letter'),
        ('Human','Bob','Dude')
    ) as 
    TestValues --equivalent of table name
    (Name, Value, Descript) --essentially field names
)
select * from actual_data_table f 
left outer join tbl_test_values d on f.Name=d.Name

有没有办法在Hive中复制上面的CTE“tbl_test_values”,即查询自定义值集作为表?

由于

1 个答案:

答案 0 :(得分:2)

1。

<强>内联

with    tbl_test_values as
        (
            select  inline
                    (
                        array
                        (
                            struct ('Number','1','One')
                           ,struct ('Number','5','Five')
                           ,struct ('Letter','A','First Letter')
                           ,struct ('Human','Bob','Dude')
                        )
                    ) as (Name, Value, Descript)
        )

select * from tbl_test_values
;
+--------+-------+--------------+
|  name  | value |   descript   |
+--------+-------+--------------+
| Number | 1     | One          |
| Number | 5     | Five         |
| Letter | A     | First Letter |
| Human  | Bob   | Dude         |
+--------+-------+--------------+

2

<强>堆

with    tbl_test_values as
        (
            select  stack
                    (
                        4
                       ,'Number' ,'1'   ,'One'
                       ,'Number' ,'5'   ,'Five'
                       ,'Letter' ,'A'   ,'First Letter'
                       ,'Human'  ,'Bob' ,'Dude'
                    ) as (Name, Value, Descript)
        )

select * from tbl_test_values
;
+--------+-------+--------------+
|  name  | value |   descript   |
+--------+-------+--------------+
| Number | 1     | One          |
| Number | 5     | Five         |
| Letter | A     | First Letter |
| Human  | Bob   | Dude         |
+--------+-------+--------------+