配置单元中的列引用无效

icomxhvb  于 2021-06-26  发布在  Hive
关注(0)|答案(3)|浏览(420)

当我运行下面的查询时,得到错误“invalid column reference:cnt”。任何建议都会很好!!

select count(customer) as cnt from (
         select customer, concat(visid, lowid), count(name) 
      from  tab1 where date_time between '2017-05-01 00:00:00' and '2017-05-31 23:59:59' and name in ('payment: Complete', 'check: Complete')
         group by evar71, concat(visid, lowid)) t1
      where cnt > 1;
cs7cruho

cs7cruho1#

另一种方法。

select count(customer) as cnt from (
         select customer, concat(visid, lowid), count(name) 
      from  tab1 where date_time between '2017-05-01 00:00:00' and '2017-05-31 23:59:59' and name in ('payment: Complete', 'check: Complete')
         group by evar71, concat(visid, lowid)) t1
    having count(customer)  > 1;
oipij1gg

oipij1gg2#

我觉得Hive更喜欢化名 group by . 此外,有几个列别名不正确:

select count(customer) as cnt
from (select customer, concat(visid, lowid) as ids, count(name) as cc
      from tab1
      where date_time >= '2017-05-01' and date_time < '2017-06-01' and
            name in ('payment: Complete', 'check: Complete')
      group by customer, ids
     ) t1
where cc > 1;
mgdq6dx1

mgdq6dx13#

WHERE 在聚合之前应用筛选器这就是原因 where cnt > 1 不起作用。有 HAVING 关键字,它在聚合后充当过滤器。

select count(customer) cnt
...
where rows_filter_condition_here --before aggregation
having count(customer) > 1       --aggregation results filter
order by cnt desc                --this works after aggregation

相关问题