运行以下代码时,我看到以下错误。
无法评估不支持的子查询类型
当我注释第三条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')
答案 0 :(得分: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表的大小,通过添加另一个对ast IN ('A', 'T')
进行过滤的CTE可能会获得一些性能提升,然后inters
CTE和主SELECT使用它们,因为它们都将读取同一张桌子两次。但是,这方面会受到满足其他约束的数据量的影响。