0

运行以下代码时,我看到以下错误。

无法评估不受支持的子查询类型

当我注释第三个 select 语句并运行查询时,查询正在运行结果。select此错误在最后一条语句中弹出。

我可以知道如何将最后一个 select 语句放在我的查询中吗?

SELECT "A1" as CUT
       ,"B1" as SERV
       ,"c1" as NAME
       ,"D1"
       ,"E1" as REP 
       ,(SELECT COUNT(ACT2."B1") 
         FROM AAA as ACT2 
         WHERE ACT2."AST" in ('A', 'T') 
            and ACT2."UCE" = 'IN' 
            and ACT2."CUST" = ACT."CUST") as NCount
       ,(SELECT ACT2."B1" 
         FROM AAA as ACT2 
         WHERE ACT2."AST" in ('A', 'T') 
            and ACT2."UCE" = 'IN' 
            and ACT2."CUST" = ACT."CUST" 
         LIMIT 1) as INTER
FROM BBB
left outer join AAA as ACT 
   on "WNB" = ACT."B1" 
WHERE "PCD" in ('PD', 'PD2') 
   and "PDT" = 0 
   and ACT."AST" in ('A', 'T')
4

1 回答 1

1

如果您正在加入可以使用 CTE 的事物,则无需运行此代码。鉴于缺少排序的b1 ANY_VALUE可以使用。

WITH inters AS (
    SELECT cust
        ,ANY_VALUE(b1) AS b1
    FROM AAA
    WHERE ast IN ('A', 'T') AND uce = 'IN' 
    GROUP BY cust
)           
SELECT a1 AS cut
    ,b1 AS serv
    ,c1 AS name
    ,d1
    ,e1 AS rep 
    ,(SELECT COUNT(ACT2."B1") FROM AAA as ACT2 WHERE ACT2."AST" in ('A', 'T') and ACT2."UCE" = 'IN' and ACT2."CUST" = ACT."CUST") as NCount
    ,i.b1 AS INTER
FROM BBB
LEFT JOIN aaa AS act 
    ON wnb = act.b1 
LEFT JOIN inters AS i
    ON i.cust = act.cust
WHERE pcd IN ('PD', 'PD2') 
    AND pdt = 0 
    AND act.ast IN ('A', 'T')           

但第一个子选择使用与秒相同的源表和过滤器,因此也可以移动到 CTE:

WITH inters AS (
    SELECT cust
        ,ANY_VALUE(b1) AS b1
        ,COINT(b1) AS ncount
    FROM AAA
    WHERE ast IN ('A', 'T') AND uce = 'IN' 
    GROUP BY cust
)           
SELECT a1 AS cut
    ,b1 AS serv
    ,c1 AS name
    ,d1
    ,e1 AS rep 
    ,i.ncount
    ,i.b1 AS INTER
FROM BBB
LEFT JOIN aaa AS act 
    ON wnb = act.b1 
LEFT JOIN inters AS i
    ON i.cust = act.cust
WHERE pcd IN ('PD', 'PD2') 
    AND pdt = 0 
    AND act.ast IN ('A', 'T')       

这应该比第一个代码更好。

根据 aaa 表的大小,添加另一个 CTE 过滤器可能会获得一些性能增益,ast IN ('A', 'T')然后intersCTE 和主 SELECT 会使用该过滤器,因为它们都会读取同一个表两次。但这方面更像会受到满足其他约束的数据量的影响。

于 2019-10-05T01:00:07.047 回答