1

问题如下。我们有两个表子查询(A 和 B)。我需要逐行应用公式
a.Processed / b.Total * 100 并将结果显示在新列“Activity %”中</p>

我尝试了以下查询:

SELECT
    (
        a.Processed / b.Total * 100
    ) AS "Activity %"
FROM
    (
        SELECT
            sla_date,
            request_type,
            CAST(
                SUM(sla_value) AS DECIMAL(
                    10,
                    2
                )
            ) AS Processed
        FROM
            table_schema.table_name
        WHERE
            team = '100'
            AND sla_duration_hrs < 480
        GROUP BY
            sla_date,
            request_type
    ) AS A 
    JOIN(
        SELECT
            sla_date,
            request_type,
            CAST(
                SUM(sla_value) AS DECIMAL(
                    10,
                    2
                )
            ) AS Total
        FROM
            table_schema.table_name
        WHERE
            team = '100'
        GROUP BY
            sla_date,
            request_type
    ) AS B
        ON a.sla_date = b.sla_date
    AND a.request_type = b.request_type  

但我收到错误

“SQL 错误 [SE001]:拼接引擎异常:为 class.splicemachine.db.impl.sql.compile.FromBaseTable 调用 ISpliceVisitor 访问方法时出现问题”

我可以使用 UNION ALL 来完成这项任务,但真正的表子查询有数百行,而且解决方案远非优雅和省时。

关于如何返工查询的任何想法?

谢谢你。

4

2 回答 2

1

请尝试以下重写,看看它是否可以解决错误:

SELECT
    sla_date,
    request_type,
    CAST(SUM(case when sla_duration_hrs < 480 then sla_value else 0 end) 
         AS DECIMAL(10,2)) /
    CAST(SUM(sla_value) AS DECIMAL(10,2)) AS  "Activity %"
FROM
    table_schema.table_name
WHERE
    team = '100'
GROUP BY
    sla_date,
    request_type;
于 2017-05-26T18:00:37.480 回答
0

It’s essentially trying to compute the % that sums of sla_value for the rows having sla_duration_hrs less than 480 over total sum of sla_value for the team ‘100’. It involves a self join. Please look at the example here http://www.1keydata.com/sql/sql-percent-to-total.html to get some ideas.

于 2017-05-26T17:07:10.003 回答