我有这个查询。应该避免嵌套选择吗?有没有更好的办法 ?
WITH cte(oi, oIdOf) AS (
SELECT ROW_NUMBER() OVER (ORDER BY resIdOf), resIdOf
FROM @res
WHERE resIdOf<>0
GROUP BY resIdOf
)
INSERT INTO @fop
SELECT x.xIdOf
,x.xIdBe
,x.xLgnBe
,(SELECT e.BE_Commercial FROM BE_ENLEVEMENT AS e WHERE e.BE_Numero_BE=x.xIdBe)
,SUM(x.xCoeff)
FROM cte AS o
CROSS APPLY dbo.ft_grapheOfOrigine(o.oIdOf) AS x
GROUP BY x.xIdOf,x.xIdBe,x.xLgnBe;