创建表查询中的嵌套选择

时间:2019-09-13 07:32:35

标签: hadoop hive hiveql

如果要与数字常量进行比较,我想编写带有嵌套select的查询的create table语句。以便使代码可读。

create table transaction_client stored
    as
select
    if(c.record_id = '01',
       'Handle',
       c.record_name) as type,
    if(c.record_id = '02',
       cast(t.amount as string),
       '')            as value,
    if(c.record_id = '02',
       c.record_name,
       '')            as attribute
from transaction  t
join       client c
           on c.id = t.client_id;

我尝试使用with结构并从表中选择代码,但不允许在行中使用不同的值。

create table classifier as
select
    stack(5,
          '01', 'CODE_01', 'TRANSACTION_TYPE',
          '02', 'CODE_02', 'TRANSACTION_TYPE',
          '03', 'CODE_03', 'TRANSACTION_TYPE'
        ) as (code, description, classifier_name)
from any_table;

create table transaction_client stored
    as orc as
with
    cl as (select code as code from classifier where classifier_name = 'TRANSACTION_TYPE' and description = 'CODE_01')
select
    if(c.record_id = cl.code,
       'Handle',
       c.record_name) as type,
    if(c.record_id = cl.code,
       cast(t.amount as string),
       '')            as value,
    if(c.record_id = cl.code,
       c.record_name,
       '')            as attribute
from transaction  t
join       client c
           on c.id = t.client_id
cross join t;

我也尝试使用地图,但是查询执行速度提高了一倍。

set hivevar:classifier=map('01', 'CODE_01',
                           '02', 'CODE_02', 
                           '03', 'CODE_03')

....
 if(c.record_id = ${classifier}['CODE_01'],
       'Handle',
       c.record_name) as type,
...

请告诉我哪种结构更好地避免了幻数,并使代码更具可读性?

0 个答案:

没有答案