cbobadillavega
10/26/2017 - 5:25 PM

Survey download

Survey download

declare @branch_code as int = 5 

;WITH SurveyResults AS (
	SELECT *, ROW_NUMBER() OVER(PARTITION BY  BranchCode, SurveyCode, CustomerCode, QuestionCode
	ORDER BY CreatedOn DESC) AS RowNumber FROM ( 
		SELECT	s.BranchCode, asu.WorkerCode , 
				asu.CustomerCode ,   
				CASE WHEN ltrim(rtrim(asr.TextResult)) != '.' THEN asr.TextResult ELSE '0' END AS TextResult, q.Text AS AnswerText, 
				s.Code AS SurveyCode, 
				s.Name AS SurveyName, 
				q.Code AS QuestionCode,
				q.QuestionId, 
				CONVERT(smalldatetime, CONVERT(char(10), asr.CreatedOn , 101))  as CreatedOn 
		FROM AssignedSurveyResult AS asr 
		INNER JOIN AssignedSurvey AS asu ON asr.AssignedSurveyId = asu.AssignedSurveyId 
		INNER JOIN Question AS q ON asu.Survey_id = q.SurveyId AND q.QuestionId = asr.QuestionId 
		INNER JOIN Survey AS s ON asu.Survey_id = s.SurveyId
		INNER JOIN BranchSyncLog b ON b.BranchCode = s.BranchCode 
		WHERE   (asu.Status = 1) 
				AND CONVERT(smalldatetime, CONVERT(char(10), asr.ModifiedOn , 101))  >= CONVERT(smalldatetime, CONVERT(char(10), b.LastSync, 101))  
				AND  (s.BranchCode = @branch_code) 
		union 
		SELECT s.BranchCode,  
				asu.WorkerCode , 
				asu.CustomerCode ,   
				CASE WHEN ltrim(rtrim(asr.TextResult)) != '.' THEN asr.TextResult ELSE '0' END AS TextResult, q.Text AS AnswerText, s.Code AS SurveyCode, 
				s.Name AS SurveyName, 
				q.Code AS QuestionCode, 
				q.QuestionId, 
				CONVERT(smalldatetime, CONVERT(char(10), asr.CreatedOn , 101))  as CreatedOn 
		FROM AssignedSurveyResult AS asr 
		INNER JOIN AssignedSurvey AS asu ON asr.AssignedSurveyId = asu.AssignedSurveyId 
		INNER JOIN Question AS q ON asu.Survey_id = q.SurveyId AND q.QuestionId = asr.QuestionId 
		INNER JOIN Survey AS s ON asu.Survey_id = s.SurveyId 
		INNER JOIN ETLInsertExceptions etl ON etl.BranchCode = s.BranchCode 
											and etl.SurveyCode = s.Code COLLATE DATABASE_DEFAULT  
											and etl.WorkerCode = asu.WorkerCode   
											and etl.CustomerCode = asu.CustomerCode  
											and asr.ModifiedOn = etl.CreatedOn  
		WHERE s.BranchCode = @branch_code
	) AS SUMMARY  
)  
select * from SurveyResults where RowNumber = 1 ORDER BY  CustomerCode , WorkerCode
declare @branch_code as int = 5 

;WITH SurveyResults AS (
	SELECT *, ROW_NUMBER() OVER(PARTITION BY  BranchCode, SurveyCode, CustomerCode, QuestionCode
	ORDER BY CreatedOn DESC) AS RowNumber FROM ( 
		SELECT	s.BranchCode, asu.WorkerCode , 
				asu.CustomerCode ,   
				CASE WHEN ltrim(rtrim(asr.TextResult)) != '.' THEN asr.TextResult ELSE '0' END AS TextResult, q.Text AS AnswerText, 
				s.Code AS SurveyCode, 
				s.Name AS SurveyName, 
				q.Code AS QuestionCode,
				q.QuestionId, 
				CONVERT(smalldatetime, CONVERT(char(10), asr.CreatedOn , 101))  as CreatedOn 
		FROM AssignedSurveyResult AS asr 
		INNER JOIN AssignedSurvey AS asu ON asr.AssignedSurveyId = asu.AssignedSurveyId 
		INNER JOIN Question AS q ON asu.Survey_id = q.SurveyId AND q.QuestionId = asr.QuestionId 
		INNER JOIN Survey AS s ON asu.Survey_id = s.SurveyId
		INNER JOIN BranchSyncLog b ON b.BranchCode = s.BranchCode 
		WHERE   (asu.Status = 1) 
				AND CONVERT(smalldatetime, CONVERT(char(10), asr.ModifiedOn , 101))  >= CONVERT(smalldatetime, CONVERT(char(10), b.LastSync, 101))  
				--AND  (s.BranchCode = @branch_code) 
		union 
		SELECT s.BranchCode,  
				asu.WorkerCode , 
				asu.CustomerCode ,   
				CASE WHEN ltrim(rtrim(asr.TextResult)) != '.' THEN asr.TextResult ELSE '0' END AS TextResult, q.Text AS AnswerText, s.Code AS SurveyCode, 
				s.Name AS SurveyName, 
				q.Code AS QuestionCode, 
				q.QuestionId, 
				CONVERT(smalldatetime, CONVERT(char(10), asr.CreatedOn , 101))  as CreatedOn 
		FROM AssignedSurveyResult AS asr 
		INNER JOIN AssignedSurvey AS asu ON asr.AssignedSurveyId = asu.AssignedSurveyId 
		INNER JOIN Question AS q ON asu.Survey_id = q.SurveyId AND q.QuestionId = asr.QuestionId 
		INNER JOIN Survey AS s ON asu.Survey_id = s.SurveyId 
		INNER JOIN ETLInsertExceptions etl ON etl.BranchCode = s.BranchCode 
											and etl.SurveyCode = s.Code COLLATE DATABASE_DEFAULT  
											and etl.WorkerCode = asu.WorkerCode   
											and etl.CustomerCode = asu.CustomerCode  
											and asr.ModifiedOn = etl.CreatedOn  
	--	WHERE s.BranchCode = @branch_code
	) AS SUMMARY  
)  
select * from SurveyResults where RowNumber = 1 ORDER BY  CustomerCode , WorkerCode