配置单元cte,我可以以表的形式查询值吗?

bnlyeluc  于 2021-05-29  发布在  Hadoop
关注(0)|答案(1)|浏览(375)

我正在尝试在查询维度表中创建我以后要在数据拉取中使用的表。在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\u test\u values”,即以表的形式查询自定义值集?
谢谢

gtlvzcf8

gtlvzcf81#

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

相关问题