在写sql时(这是主要指hive应用场景),比较复杂的sql会写的比较长,有时候是几百行的sql,可读性差且不易debug问题。所以,如果把中间结果保存到临时表里则逻辑相对清晰。样例如下:
-- 临时表 with alias_tbl1 as (select col1, col2 from table_name1 where condition1 ), -- 注意此处如果需要接着定义其他临时表,则需逗号隔开 alias_tbl2 as -- 连续定义,这里不需“with” (select col_1, col_2 from table_name2 where condition_2 ) -- 在以上基础上,使用两张临时表,这里以join为例。 select alias_tbl1.col1 as terminal_col1, alias_tbl2.col_2 as terminal_col2 from alias_tbl1 join alias_tbl2 on alias_tbl1.col1=alias_tbl2.col_1



