创建表查询中的嵌套select

k2arahey  于 2021-05-29  发布在  Hadoop
关注(0)|答案(0)|浏览(254)

我想写CREATETABLE语句,用if结构中嵌套的select查询来与数字常量进行比较。以便代码可读。

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;

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

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,
...

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

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题