我正在尝试查询特定的格式化日期:

我有这个查询:

SELECT
    REGEXP_REPLACE(datewithoutdash,
    '^(\\d{2})(\\d{2})(\\d{2})(.*)$','20\\1-\\2-\\3')
    datewithdash
     FROM table1 WHERE datewithdash < "2016-11-10";

为什么我不能在新变量的子句中使用

我收到此错误:

最佳答案

Hive在同一查询中评估where子句时,不知道select子句中的别名列名称。不幸的是,您必须嵌套它,或将转换函数复制到where子句中:

SELECT
    REGEXP_REPLACE(datewithoutdash,
    '^(\\d{2})(\\d{2})(\\d{2})(.*)$','20\\1-\\2-\\3') as datewithdash
FROM
     table1
WHERE
    REGEXP_REPLACE(datewithoutdash,
    '^(\\d{2})(\\d{2})(\\d{2})(.*)$','20\\1-\\2-\\3')  < "2016-11-10";

要么
select * from (
    SELECT
        REGEXP_REPLACE(datewithoutdash,
        '^(\\d{2})(\\d{2})(\\d{2})(.*)$','20\\1-\\2-\\3') as datewithdash
    FROM
         table1
    ) a
WHERE
    datewithdash  < "2016-11-10";

另一个注意事项-该函数非常讨厌-您可能会使用以下 hive 函数构建:
to_date(unix_timestamp(datewithoutdash,'yyMMdd'))

相反-可能更清楚。

08-28 05:56