declare @UsedItems Table
( itemid int primary key, contentID int )
insert into @UsedItems
Select ItemID, ContentID from [vefn_GetVersionItemsIncludingDeleted]('')

select 'Working Drafts' Query, FolderName,Dv.* from DocVersions DV
JOIN VEFN_GetVersionNames() VN ON DV.VersionID = VN.VersionID

declare @DiscItems Table
( itemid int primary key, ContentID int)
insert into @DiscItems
Select ItemID, II.ContentID from tblItems II
Join tblContents CC ON CC.ContentID = II.ContentID
where ItemID not in(select ItemID from @UsedItems)
And CC.Type is not null

select 'Total Disconnected Items' Query, count(*) HowMany from @DiscItems
--select COUNT(*) HowManyTotal from Items II
--Join Contents CC ON CC.ContentID = II.ContentID
--where ItemID in (select ItemID from @DiscItems)

-- Disconnected Groups
select 'Disconnected Groups' Query, ItemID,II.ContentID, Type, replace(Number,'\u8209?','-') Num, Text, CC.UserID, CC.DTS
, (select COUNT(*) from vefn_SiblingItems(ItemID,null))-1 Siblings
, (select COUNT(*) from vefn_SiblingChildrenItems(ItemID))-1 SiblingChildren
, (select MAX(DTS) from vefn_SiblingChildrenItems(ItemID) VI join Contents CC on VI.ContentID = CC.ContentID) ContentDTS
, (select MAX(DTS) from vefn_SiblingChildrenItems(ItemID) VI join Items II on VI.ItemID = II.ItemID) ItemDTS
, (select MIN(ItemID) from vefn_SiblingChildrenItems(ItemID)) MinItemID
, (select MAX(ItemID) from vefn_SiblingChildrenItems(ItemID)) MaxItemID
, (select COUNT(*) from transitions 
 where fromid in(select ContentID from vefn_SiblingChildrenItems(ItemID)) 
 and toid in (select ItemID from @UsedItems)) FromDiscTrans
, (select COUNT(*) from transitions 
 where fromid in(select ContentID from @UsedItems) 
 and toid in (select ItemID from vefn_SiblingChildrenItems(ItemID))) ToDiscTrans
, (Select Count(*) from Annotations AA
 where AA.ItemID In (select ItemID from vefn_SiblingChildrenItems(II.ItemID))) Annotations
, (Select Count(*) from ROUsages
 where ContentID In (select ContentID from vefn_SiblingChildrenItems(ItemID))) RefObjects
, (Select Count(*) from Entries
 where ContentID In (select ContentID from vefn_SiblingChildrenItems(ItemID))) LibraryDocs
from Items II
Join Contents CC ON CC.ContentID = II.ContentID
where ItemID in (select ItemID from @DiscItems)
and PreviousID is null
and ItemID not in (Select ItemID from Parts)