DECLARE @LogDate AS BIGINT = dbo.GetUnixTime(GETUTCDATE()) 
DECLARE @queryId AS INTEGER = 10232
DECLARE @surveyXML nvarchar(MAX) 
 
DECLARE @releaseId integer 
SET @releaseId = (select releaseId from APP_Client where id = 2) 
 
DECLARE @temptbl table 
(
[PackageId] nvarchar(max),[Package] nvarchar(max),[Number of clients] int
)


IF @releaseId = 15 OR @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; SELECT CA._ID  AS PackageId,
        SP.name as Package,
		COUNT(*) AS [Number of clients]
 FROM APP_ClientProp CP (NOLOCK) CROSS APPLY dbo.SplitStringByDelimiter(attrVal, '','') CA
   INNER JOIN SimPackage (NOLOCK) SP ON SP.id = CA._ID
 WHERE attrname = ''AutomaticPackagesInstalled'' and modified = 0 
 GROUP BY CA._ID, SP.name ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [PackageId] AS '@PackageId',[Package] AS '@Package',[Number of clients] AS '@Col_-1229951530' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10232'), 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')) 
