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


IF @releaseId = 16
BEGIN 
 insert into @temptbl 
EXEC('SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED; SET NOCOUNT ON; select r.name As Report, ISNULL([Views], 0) As [Views]
From APP_Reports r with(nolock) 
LEFT OUTER JOIN (
	Select reportId, sum([Views]) As [Views] From APP_ReportActivity with(nolock) Group By reportId
) v ON r.reportId = v.reportId
Where r.reportId in (Select reportId From APP_ReportsHistory with(nolock) Where LEN(location) > 0) ') 
 END
 
 SET @surveyXML = 
( SELECT 
(
SELECT [Report] AS '@Report',[Views] AS '@Views' FROM @temptbl
 FOR XML PATH ('cf_MetricsQuery10359'), 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')) 
