我正在尝试运行如下所示的查询。我们的目标是获得每user_key
的总活动数,但因为user_key
具有复杂的结构,我只需要在' |'之后的部分。符号我必须使用子字符串函数。但是,当我试图运行查询时,我得到了
错误:
SQL Error [42Y36]: Column reference 'USER_KEY' is invalid, or is part of an invalid expression. For a SELECT list with a GROUP BY, the columns and expressions being selected may only contain valid grouping expressions and valid aggregate expressions.
子串函数在此查询之外正常工作。有没有解决这个问题的方法?使用Splice Machine(NoSql)
SELECT
substr(user_key, instr(user_key,'|') + 1) AS new_user_key,
SUM(
CAST(
activity_count AS INTEGER
)
) AS Total
FROM
schema_name.table_name
GROUP BY
substr(user_key, instr(user_key,'|') + 1)
答案 0 :(得分:0)
您的GROUP BY列需要与SELECT
匹配SELECT
substr(user_key, instr(user_key,'|') + 1) AS new_user_key,
SUM(
CAST(
activity_count AS INTEGER
)
) AS Total
FROM
schema_name.table_name
GROUP BY
substr(user_key, instr(user_key,'|') + 1) AS new_user_key
答案 1 :(得分:0)
我自己找到了答案。我使用了一个表子查询:
SELECT new_table.new_user_key, sum(new_table.total)
from
(
SELECT
substr(user_key, instr(user_key,'|') + 1) AS new_user_key,
CAST(activity_count AS INTEGER) AS Total
FROM schema_name.table_name
)
as new_table
GROUP BY
new_table.new_user_key
让我们希望有人会发现这篇文章很有用,并且可以为他或她节省一些时间。