SUM和GROUP BY Splice中的子字符串(NoSql)

时间:2017-05-09 15:28:33

标签: sql nosql

我正在尝试运行如下所示的查询。我们的目标是获得每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)

2 个答案:

答案 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

让我们希望有人会发现这篇文章很有用,并且可以为他或她节省一些时间。