ClickHouse SQL 语法错误:arrayEnumerate 函数使用错误
在 ClickHouse SQL 中,arrayEnumerate 函数必须在 FROM 语句的子查询中使用,不能直接在 FROM 语句中使用。
错误代码:
SELECT t,
arraySum(arraySlice(total, 1, i)) as total_num
FROM
(
SELECT groupArray(min_Ds) as t,
groupArray(app_count) as total
from (
SELECT min_Ds,
count(distinct AppIdentifier) as app_count
FROM (
select AppIdentifier,
min(Ds) as min_Ds
from log_iMonkey_iOS_overview
GROUP BY AppIdentifier
)
GROUP BY min_Ds
order by min_Ds
)
)
arrayEnumerate(total) as i where t >= toDate('#accumulated_access_services.start#')
错误信息:
errormessage: 运行SQL失败:[400]运行异常: Code: 62. DB::Exception: Syntax error: failed at position 475 ('(') (line 20, col 17): (total) as i where t >= toDate('2021-11-27 00:00:00') LIMIT 100000. Expected one of: FINAL, SAMPLE, table, table function, subquery or list of joined tables, array join, LEFT ARRAY JOIN, INNER, ARRAY JOIN, JOIN, PREWHERE, WHERE, GROUP BY, WITH, HAVING, WINDOW, ORDER BY, LIMIT, OFFSET, SETTINGS, UNION, EXCEPT, INTERSECT, INTO OUTFILE, FORMAT, end of query. (SYNTAX_ERROR) (version 52.3.10.8)
解决方案:
在 arrayEnumerate(total) 前加上一个逗号,将其放在 FROM 语句的子查询中。
修改后的 SQL 代码:
SELECT t,
arraySum(arraySlice(total, 1, i)) as total_num
FROM
(
SELECT groupArray(min_Ds) as t,
groupArray(app_count) as total
from (
SELECT min_Ds,
count(distinct AppIdentifier) as app_count
FROM (
select AppIdentifier,
min(Ds) as min_Ds
from log_iMonkey_iOS_overview
GROUP BY AppIdentifier
)
GROUP BY min_Ds
order by min_Ds
),
arrayEnumerate(total) as i
where t >= toDate('#accumulated_access_services.start#')
)
LIMIT 100000;
经过修改后,代码能够正常运行,并返回期望的结果。
原文地址: https://www.cveoy.top/t/topic/oz7g 著作权归作者所有。请勿转载和采集!