select vn.versionid, FolderName, aa.rofstid, rf.rodbid, rf.dts, rd.Roname, rd.folderpath, ru.rodbid crodbid, count (*) howmany
from vefn_getversionnames() vn
join vefn_getversionitems('22,25,26,30') vi on vn.versionid = vi.versionid
join rousages ru on ru.contentid = vi.contentid
join associations aa on aa.versionid = vn.versionid
join rofsts rf on rf.rofstid = aa.rofstid
join rodbs rd on rf.rodbid = rd.rodbid
where rd.rodbid <> ru.rodbid
group by vn.versionid, FolderName, aa.rofstid, rf.rodbid, rf.dts, rd.Roname, rd.folderpath, ru.rodbid
order by rf.dts