我的配置单元sql:
select id from Table
where date>'20180101'
and date<'20190101'
group by id
having count(distinct substr(date,5,2))=7
并返回错误:无效的列引用“ 2”
为什么以及如何解决?
答案 0 :(得分:0)
尝试以下,
查询希望聚合函数成为select子句的一部分。
with table1 as (select 1 as id, '20180101' as date1 union select 1 as id, '20110101' as date1 union select 1 as id, '20190101' as date1 union select 2 as id, '20180101' as date1 union select 3 as id, '20180102' date1) select id, count(distinct substr(date1,5,2)) from table1 where date1>'20180101' and date1<'20190101' group by id having count(distinct substr(date1,5,2))=7;
以下查询有效- 我们只能在具有having子句中具有聚合功能,但必须仅是count(*)
select id from table1 where date1>'20180101' and date1<'20190101' group by id having count(date1)=7;
但是下面的查询抛出错误-
select id from table1 where date1>'20180101' and date1<'20190101' group by id having count(distinct (date1))=7;
FAILED: SemanticException [Error 10002]: Line 1:319 Invalid column reference 'date1'
希望这会有所帮助