sql-server - Hive CTE,我可以将值查询为表吗?

标签 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.

内联

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

关于sql-server - Hive CTE,我可以将值查询为表吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43243669/

相关文章:

sql - 我应该设计一个 SQL Server 数据库来依赖 UNION 还是避免它?

sql - 如何从没有键的 JSON 字符串中提取值?

java - 无法在Maven中的Oozie-hadoop项目上执行目标以安装Oozie

java - 组合器在HBase扫描mapreduce中为每个区域创建mapoutput文件

sql - Hive 和 SparkSQL 不支持日期时间类型?

apache-spark - Spark 中的 HIVE Metastore 警告

sql - 根据 SQL 中的下一条记录和上一条记录进行排序

SQL Server : Search for a string across all the databases and list all the databases, 表和对应的列

hadoop - hive 查询 : Is there a way to use UDTF with `cluster by` ?

hadoop - 直线无法连接到 hiveserver2