Hive如何查询转换后的变量; FAILED:SemanticException [错误10004]

时间:2016-11-30 18:20:19

标签: hadoop hive

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

我有这个问题:

public static void main(String[] args) {
        double  line;
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the number of Triangles:");
        line = s.nextInt();

        int side[] = new int[3];//because every triangle has 3 sides
         double min = Double.MAX_VALUE;//assuming perimeter of a tringle will be grater than zero
        while(line-->0){//we need to loop as many no Triangles
            System.out.println("Please, insert lengths of the sides of this triangles(3 real numbers per line) ");
       // for (int i = 0; i < side.length; i++) {// for reading array
            side[0] = (int) s.nextDouble();
            side[1] = (int) s.nextDouble();
            side[2] = (int) s.nextDouble();
                        double perimeter = side[0]+side[1]+side[2];
                        if(perimeter<min){
                           min = perimeter;
                        }
                        System.out.println(perimeter);
                        //System.out.println("Enter the next  Triangles:");
        //}
        }
        System.out.println("Minimum Perimeter :"+min);
       // line--;

    }

为什么我不能在新变量上使用 where 子句?

我收到了这个错误:

  

FAILED:SemanticException [错误10004]:第26:14行无效表   别名或列引用'datewithdash':(可能的列名是:   ...)

2 个答案:

答案 0 :(得分:3)

当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";

OR

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'))
相反 - 它可能更清楚。

答案 1 :(得分:0)

Hive无法识别别名。你需要再次重复整个表达。