Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过拼接中的子字符串求和和和分组(NoSql)_Sql_Nosql - Fatal编程技术网

通过拼接中的子字符串求和和和分组(NoSql)

通过拼接中的子字符串求和和和分组(NoSql),sql,nosql,Sql,Nosql,我正在尝试运行下面的查询。目标是获得每个user_key的总活动计数,但由于user_key具有复杂的结构,我只需要“|”符号后面的部分,因此我不得不使用子字符串函数。然而,当我试图运行查询时,我得到 错误: 子字符串函数在此查询外部正常工作。这个问题有解决办法吗?使用拼接机(NoSql) “分组方式”列需要与“选择”列匹配 SELECT substr(user_key, instr(user_key,'|') + 1) AS new_user_key, SUM(

我正在尝试运行下面的查询。目标是获得每个
user_key
的总活动计数,但由于
user_key
具有复杂的结构,我只需要“|”符号后面的部分,因此我不得不使用子字符串函数。然而,当我试图运行查询时,我得到 错误:

子字符串函数在此查询外部正常工作。这个问题有解决办法吗?使用拼接机(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) AS new_user_key

我自己找到了答案。我使用了表子查询:

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

我们希望有人会觉得这篇文章很有用,并为他或她节省一些时间。

这没用。不允许向分组依据表达式添加别名。
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
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