WITH ctetest(AgencyID,ParentAgencyID,level)AS
(
SELECT AgencyID,ParentAgencyID,0 level FROM dbo.Web_Agency WHERE AgencyID=1
UNION ALL
SELECT a.AgencyID,a.ParentAgencyID,b.level+1 FROM dbo.Web_Agency a,ctetest b WHERE a.ParentAgencyID=b.AgencyID
)
SELECT DISTINCT u.UserID FROM ctetest c INNER JOIN dbo.TUsers u ON c.AgencyID=u.AgencyID