﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="d347bbe5-f7e2-45f4-8f0f-baf64960fbdf" codekey="ADAC_QuestionstatisticsWithDateRange" categoryCodekey="TestsAndQuestions" name="Fragenstatistik Mandant (mit Zeitraumsangabe)" description="">
      <MetaData created="2011-12-14T16:48:46" createdBy="Administrator Zentrale" createdBy_user_id="3" modified="2016-07-28T10:43:29" modifiedBy="Gabi Wappler" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="ExcludeMandators" mandator_id="cf85efc9-150b-4eee-8c20-929a112b658c" mandatorName="ADAC" isStandard="False" isUsedByMenu="False" />
      <Parameters>
        <Parameter id="a785aecc-668a-4609-9613-394bd4d708c2" isRequired="False" allowMultiSelect="False" name="Mandant" contextName="Mandant" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isRequired="False" allowMultiSelect="False" name="Zeitraum" contextName="Zeitraum" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles />
      <command>-- Schlüssel für Code: 
-- Name:               Fragenstatistik Mandant (Zeitraum)
-- Beschreibung:       
-- Ergebnisformat:     Tabelle
-- Für alle Mandanten: Ja
-- Verfügbar für:      ?

set @dateRange_end = dateadd(day,1,@dateRange_end)

CREATE TABLE #Result
(
	id INT IDENTITY(1,1),
	type INT,
	numberSuffix INT,
	lernzielname_intern varchar(512),
	question_Id UNIQUEIDENTIFIER,
	answered_Count INT,
	correct_count INT,
	not_answered_Count INT,
	answerA_Count INT,
	answerB_Count INT,
	answerC_Count INT,
	answerD_Count INT,
	answerE_Count INT,
	answerF_Count INT,
	answerG_Count INT,
	answerH_Count INT,
	answerI_Count INT,
	answerJ_Count INT
) 
CREATE TABLE #ResultOutput
(
	numberSuffix INT,
	LernzielnameIntern varchar(512),
	Fragenbaumstruktur varchar(512),
	beantwortet INT,
	richtig_beantwortet INT,
	falsch_beantwortet INT,
	nicht_beantwortet INT,
	Antwort_A INT,
	Antwort_B INT,
	Antwort_C INT,
	Antwort_D INT,
	Antwort_E INT,
	Antwort_F INT,
	Antwort_G INT,
	Antwort_H INT,
	Antwort_I INT,
	Antwort_J INT
) 
INSERT INTO #Result
SELECT 
		(SELECT type FROM tblTasQuestions WHERE id=tblTasStatsTestSessionQuestions.question_id) type,
		(SELECT numberSuffix FROM tblTasQuestions WHERE id=tblTasStatsTestSessionQuestions.question_id) numberSuffix,
		'' AS lernzielname_intern,
		tblTasStatsTestSessionQuestions.question_id AS question_Id,
		(select COUNT(correct) from tblTasStatsTestSessionQuestions t1 
			INNER JOIN tblTasStatsTestSessions ON t1.testsession_id = tblTasStatsTestSessions.testsession_id
			INNER JOIN tblTasTests ON tblTasStatsTestSessions.test_id = tblTasTests.id
			where t1.question_Id = tblTasStatsTestSessionQuestions.question_id AND 
			tblTasStatsTestSessions.visible = 1
			AND (@dateRange_start is null or tblTasStatsTestSessions.endDate&gt;=@dateRange_start)
			AND (@dateRange_end is null or tblTasStatsTestSessions.endDate&lt;@dateRange_end)
			AND tblTasTests.mode = 1) 
			- SUM( CASE WHEN answer_index=0 THEN 1 ELSE 0 END) 
			AS answered_Count, 
		(select SUM(correct) from tblTasStatsTestSessionQuestions t1 
			INNER JOIN tblTasStatsTestSessions ON t1.testsession_id = tblTasStatsTestSessions.testsession_id
			INNER JOIN tblTasTests ON tblTasStatsTestSessions.test_id = tblTasTests.id
			where t1.question_Id = tblTasStatsTestSessionQuestions.question_id AND 
			tblTasStatsTestSessions.visible = 1 AND
			tblTasTests.mode = 1
			AND (@dateRange_start is null or tblTasStatsTestSessions.endDate&gt;=@dateRange_start)
			AND (@dateRange_end is null or tblTasStatsTestSessions.endDate&lt;@dateRange_end)
		) 
			AS correct_Count, 
		SUM( CASE WHEN tblTasQuestions.type in (1,2,3,6) and answer_index IS NULL THEN 1 ELSE 0 END) AS not_answered_Count,
		SUM( CASE WHEN answer_index=1 THEN 1 ELSE 0 END) AS answerA_Count,
		SUM( CASE WHEN answer_index=2 THEN 1 ELSE 0 END) AS answerB_Count,
		SUM( CASE WHEN answer_index=3 THEN 1 ELSE 0 END) AS answerC_Count,
		SUM( CASE WHEN answer_index=4 THEN 1 ELSE 0 END) AS answerD_Count,
		SUM( CASE WHEN answer_index=5 THEN 1 ELSE 0 END) AS answerE_Count,
		SUM( CASE WHEN answer_index=6 THEN 1 ELSE 0 END) AS answerF_Count,
		SUM( CASE WHEN answer_index=7 THEN 1 ELSE 0 END) AS answerG_Count,
		SUM( CASE WHEN answer_index=8 THEN 1 ELSE 0 END) AS answerH_Count,
		SUM( CASE WHEN answer_index=9 THEN 1 ELSE 0 END) AS answerI_Count,
		SUM( CASE WHEN answer_index=10 THEN 1 ELSE 0 END) AS answerJ_Count
	FROM  tblTasStatsTestSessionQuestions
		INNER JOIN tblTasQuestions
		ON tblTasStatsTestSessionQuestions.question_id = tblTasQuestions.id  
		INNER JOIN tblTasStatsTestSessions ON tblTasStatsTestSessionQuestions.testsession_id = tblTasStatsTestSessions.testsession_id
		INNER JOIN tblTasTests ON tblTasStatsTestSessions.test_id = tblTasTests.id
		LEFT JOIN tblTasStatsQuestionsChoice ON (tblTasStatsTestSessionQuestions.testsession_id=tblTasStatsQuestionsChoice.testsession_id AND tblTasStatsTestSessionQuestions.question_id=tblTasStatsQuestionsChoice.question_id AND tblTasStatsQuestionsChoice.selected=1)
	WHERE
		tblTasStatsTestSessions.visible = 1
		AND tblTasTests.mode = 1
		AND dbo.tblTasQuestions.deleted IS NULL
		AND tblTasQuestions.mandator_id = @mandator_id
		AND (@dateRange_start is null or tblTasStatsTestSessions.endDate&gt;=@dateRange_start)
		AND (@dateRange_end is null or tblTasStatsTestSessions.endDate&lt;@dateRange_end)
	GROUP BY tblTasStatsTestSessionQuestions.question_id

-- correct not answered count due to missing entries
update #Result set not_answered_Count = answered_Count - correct_Count where not_answered_Count &gt; answered_Count - correct_Count

--Remove the answer alternatives fpr types 4, 6 and 7
update #Result
set 
	answerA_Count = 0,
	answerB_Count = 0,
	answerC_Count = 0,
	answerD_Count = 0,
	answerE_Count = 0,
	answerF_Count = 0,
	answerG_Count = 0,
	answerH_Count = 0,
	answerI_Count = 0,
	answerJ_Count = 0
WHERE
	type=2 OR type=4 OR type=6 OR type=7

--Now fetch the category tree
DECLARE @au_id char( 11 )
SELECT @au_id = min( id ) FROM #Result
WHILE @au_id IS NOT NULL
BEGIN
   
	INSERT INTO #ResultOutput
		SELECT
		dbo.tblTasQuestions.numberSuffix AS Fragennummer,
		T1.internal_name AS LernzielnameIntern,
	   (SELECT 
		CASE WHEN dbo.fn_GetResourceString(T10.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T10.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T9.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T9.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T8.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T8.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T7.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T7.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T6.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T6.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T5.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T5.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T4.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T4.name_resource_id,1031,1,1031) + '/' END + 
		CASE WHEN dbo.fn_GetResourceString(T1.name_resource_id,1031,1,1031) IS NULL THEN '' ELSE dbo.fn_GetResourceString(T1.name_resource_id,1031,1,1031) END
		FROM tblTasQuestionCategories AS T2 
		LEFT OUTER JOIN tblTasQuestionCategories AS T4
		ON (T4.ID = T2.parent_id AND (t4.parent_id IS NOT NULL AND t4.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T5
		ON (T5.ID = t4.parent_id AND (t5.parent_id IS NOT NULL  AND t5.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T6
		ON (T6.ID = t5.parent_id AND (t6.parent_id IS NOT NULL AND t6.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T7
		ON (T7.ID = t6.parent_id AND (t7.parent_id IS NOT NULL AND t7.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T8
		ON (T8.ID = t7.parent_id AND (t8.parent_id IS NOT NULL AND t8.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T9
		ON (T9.ID = t8.parent_id AND (t9.parent_id IS NOT NULL AND t9.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T10
		ON (T10.ID = t9.parent_id AND (t10.parent_id IS NOT NULL AND t10.specialPurpose &lt;&gt;2))
		LEFT OUTER JOIN tblTasQuestionCategories AS T11
		ON (T11.ID = t10.parent_id AND (t11.parent_id IS NOT NULL AND t11.specialPurpose &lt;&gt;2))
		WHERE 
		T2.ID = T1.Id
		) AS Fragenbaumstruktur,
		/*#Result.question_Id,*/
		#Result.answered_Count AS beantwortet,
		#Result.correct_count AS richtig_beantwortet,
		sum(#Result.answered_Count-#Result.correct_count-#Result.not_answered_Count) AS falsch_beantwortet, 
		#Result.not_answered_Count AS nicht_beantwortet,
		ABS(#Result.answerA_Count) AS Antwort_A,
		ABS(#Result.answerB_Count) AS Antwort_B,
		ABS(#Result.answerC_Count) AS Antwort_C,
		ABS(#Result.answerD_Count) AS Antwort_D,
		ABS(#Result.answerE_Count) AS Antwort_E,
		ABS(#Result.answerF_Count) AS Antwort_F,
		ABS(#Result.answerG_Count) AS Antwort_G,
		ABS(#Result.answerH_Count) AS Antwort_H,
		ABS(#Result.answerI_Count) AS Antwort_I,
		ABS(#Result.answerJ_Count) AS Antwort_J
		FROM #Result 
		INNER JOIN dbo.tblTasQuestionCategoryQuestions
		ON
		dbo.tblTasQuestionCategoryQuestions.question_id = #Result.question_id
		INNER JOIN 
		dbo.tblTasQuestionCategories AS T1
		ON dbo.tblTasQuestionCategoryQuestions.category_Id = T1.id
		INNER JOIN 
		dbo.tblTasQuestions
		ON 
		dbo.tblTasQuestionCategoryQuestions.question_id = tblTasQuestions.id
		LEFT OUTER JOIN
		dbo.tblTasQuestionCategories AS TParent
		ON T1.parent_id = TParent.ID
		WHERE #Result.id = @au_id
		AND T1.parent_id IS NOT NULL
		AND T1.specialPurpose &lt;&gt; 4
		AND dbo.fn_GetResourceString(TParent.name_resource_id,1031,1,1031) IS NOT NULL
		AND T1.categoryType = 3
		GROUP BY #Result.question_id,
		#Result.answered_Count,
		#Result.correct_count,
		#Result.not_answered_Count,
		#Result.answerA_Count,
		#Result.answerB_Count,
		#Result.answerC_Count,
		#Result.answerD_Count,
		#Result.answerE_Count,
		#Result.answerF_Count,
		#Result.answerG_Count,
		#Result.answerH_Count,
		#Result.answerI_Count,
		#Result.answerJ_Count,
		dbo.tblTasQuestions.numberSuffix, T1.internal_name, T1.ID, T1.specialPurpose, T1.name_resource_id
	SELECT @au_id = min( id ) FROM #Result where id &gt; @au_id
	END

SELECT * FROM  #ResultOutput 
ORDER BY fragenbaumstruktur
DROP TABLE  #Result
DROP TABLE #ResultOutput

</command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="a785aecc-668a-4609-9613-394bd4d708c2" isSystem="True" name="Mandant" reportParameterType_id="6b8fa242-5449-45b3-a81a-65a7e5e3b51d" queryParameterName="@mandator_id" />
    <Parameter id="d8e4595f-8bdd-4a54-9298-8f378450e6a0" isSystem="True" name="Zeitraum" reportParameterType_id="abff13be-91c3-4ee1-93a3-7292f8e013ba" queryParameterName="@dateRange" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="6b8fa242-5449-45b3-a81a-65a7e5e3b51d" isSystem="True" name="Mandator" datatype="Mandator" dataValueField="" dataTextField="" />
    <ParameterType id="abff13be-91c3-4ee1-93a3-7292f8e013ba" isSystem="True" name="DateRange" datatype="DateRange" dataValueField="" dataTextField="" />
  </ParameterTypes>
</ReportsExport>