DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10010
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @releaseId integer 
SET @releaseId = (select releaseId from APP_Client where id = 2) 
 
DECLARE @temptbl table 
(
[DLOClients] int, [ShareUserCount] int, [EdgeDriveUserCount] int
)


IF @releaseId >= 15 
BEGIN 
 insert into @temptbl 
EXEC('SET NOCOUNT ON; SELECT 
         (SELECT COUNT(*) FROM APP_Client WHERE status & 4096 = 4096) AS ''DLOClients''
        ,(SELECT COUNT(DISTINCT ownerId) ''share folders'' FROM 
        App_SyncCloudFolder WITH (NOLOCK)
        WHERE flag&4 <> 0 OR flag&8 <> 0) AS ''ShareUserCount''
        ,(SELECT COUNT(*) FROM APP_BackupSetProp WHERE attrName = ''Edge Drive User Id'' and modified = 0) AS ''EdgeDriveUserCount''
 ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
Select [DLOClients] AS '@DLOClients',[ShareUserCount] AS '@ShareUserCount',[EdgeDriveUserCount] AS '@EdgeDriveUserCount' from @temptbl
 FOR XML PATH ('cf_MetricsQuery10010'), type
)
  FOR XML PATH ('SurveyResults') 
)

DECLARE @EndTime AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
SET @outputXML = 
( 
    SELECT 
         @queryId AS '@QueryId', 
         @EndTime AS '@LogDate', 
     (@EndTime - @LogDate) AS '@QueryRunningTime', 
@surveyXML     FOR XML PATH('Rpt_CSSXMLDATA') 
) 
