From your posting, it is not clear the construct you are trying to use, and what table is aliased to b. Perhaps the following? If that is not it, posting a more representative query with sample data would help.
SELECT
a.name,
b.data
FROM
destr a
OUTER APPLY
(
SELECT x.data
FROM
sometable AS x
WHERE
x.field = a.field
) AS b
(Select an.dataid from dtreeancestors an Where an.ancestorid=2000)
AND
(A2.See=3 OR A2.See=4)
AND
A3.Deleted=0
),
GroupMembers (ParentGroup, Name, ID, Type, Level) AS
(
SELECT KUAF.Name, KUAF.Name, KUAF.ID, KUAF.Type, 0 AS Level FROM KUAF WHERE ID IN (Select RightID from Objects)
UNION ALL
SELECT GP.Name, KUAF.Name, KUAF.ID, KUAF.Type, Level+1 AS Level FROM KUAF
INNER JOIN KUAFChildren KC ON KC.ChildID = KUAF.ID
INNER JOIN GroupMembers GP ON GP.ID = KC.ID
)
Select count(RightID) AS "Number User Write Permissions" FROM
(
Select RightID from Objects where Type=0
UNION
Select ID AS RightID from GroupMembers where Type=0
) t
This works perfect, but only for one ID as you can see in this term: an.ancestorid=2000
No I have a list of Id's and want do the whole operation for each ID. This is the source...
WITH ancestors
AS (
SELECT an.dataid
FROM dtreeancestors an
WHERE an.ancestorid IN (SELECT ID FROM LIST)
)
, Objects AS (
...
...
{replace (Select an.dataid from dtreeancestors an Where an.ancestorid=2000) with (Select an.dataid From ancestors an)
...