﻿<?xml version="1.0" encoding="utf-8"?>
<ReportsExport>
  <Reports>
    <Report id="1b90389d-8ca7-4445-98f3-af822045eab1" codekey="UsersTestResultsByTG" categoryCodekey="UserStats" name="Users with test results by target group" description="List of users with test results by target group">
      <MetaData created="2014-11-13T13:38:55" createdBy="Administrator BWB (Administrator)" createdBy_user_id="19948" modified="2014-11-13T16:08:10" modifiedBy="Administrator BWB" />
      <ExecutionDetails format="TableResult" commandType="SqlCommandOrQuery" exportHandler="" adminControl="" exportMultipleTablesToSheets="False" datesWithTime="False" extraParams="" />
      <Mandators mandatorMode="IncludeMandators" mandator_id="c827feb3-aa67-4f1e-bd95-7214c471f191" isStandard="True" isUsedByMenu="False">
        <Mandator mandatorName="BWB" />
        <Mandator mandatorName="TestAllFeatures" />
        <Mandator mandatorName="VIWIS" />
      </Mandators>
      <Parameters>
        <Parameter id="4aaf4f45-f70a-452a-92b5-f8e9b8b0dfb6" isRequired="True" name="Test" contextName="Test" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
        <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isRequired="True" name="Zielgruppe" contextName="Zielgruppe" defaultValue="" renderHint="Undefined" disableParameter="DontDisable" />
      </Parameters>
      <Roles>
        <Role id="90" />
      </Roles>
      <command>select title as 'Test' from tblItems where id = @test_id
select title as 'Zielgruppe' from tblTargetGroups where id = @targetGroup_id

if(exists(select * from tblTasTests where id=@test_id and resultCalculationMode=1))
begin
  CREATE TABLE #SGS(id uniqueidentifier, code nvarchar(10), name nvarchar(100))
  insert into #SGS
  select 
        subjectGroupId as 'id',
		'SG' + cast(ROW_NUMBER() OVER(ORDER BY text) as varchar) AS 'code',
        text as 'name' 
  from tblTasTestSessionQuestions
      inner join tblTasStatsTestSessions on tblTasStatsTestSessions.testsession_id = tblTasTestSessionQuestions.testSessionId
	  inner join tblUsersTargetGroups on tblTasStatsTestSessions.user_id = tblUsersTargetGroups.UserCn
      inner join tblTasQuestionCategories on tblTasTestSessionQuestions.subjectGroupId = tblTasQuestionCategories.id
      inner join tblResources on name_resource_id = tblResources.id
  where tblTasStatsTestSessions.test_id = @test_id and tblUsersTargetGroups.targetGroup_id = @targetGroup_id
  group by subjectGroupId, text
  order by text

  select 
	code as 'Sachgebiete',
	name as 'Name'
  from #SGS

  select 
        Nachname as 'Name',
        Vorname as 'Vorname',
        descUserCn as 'Kennung',
        case when passed = 1 then 'Bestanden' else 'nich Bestanden' end as 'Ergebnis',
        pctQuestions as 'Prozentsatz gesamt',
		endDate as 'Datum',
        case when exists(select * from #SGS where code='SG1') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG1'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG1'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG1',
        case when exists(select * from #SGS where code='SG2') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG2'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG2'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG2',
        case when exists(select * from #SGS where code='SG3') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG3'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG3'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG3',
        case when exists(select * from #SGS where code='SG4') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG4'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG4'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG4',
        case when exists(select * from #SGS where code='SG5') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG5'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG5'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG5',
        case when exists(select * from #SGS where code='SG6') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG6'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG6'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG6',
        case when exists(select * from #SGS where code='SG7') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG7'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG7'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG7',
        case when exists(select * from #SGS where code='SG8') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG8'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG8'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG8',
        case when exists(select * from #SGS where code='SG9') then 
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG9'
			where testsession_id = tblTasStatsTestSessions.testsession_id and correct=1) as varchar) + '/' +
			cast((select count(*) from tblTasStatsTestSessionQuestions t1 
				inner join tblTasTestSessionQuestions t2 on t1.testsession_id=t2.testSessionId and t1.question_id=t2.questionId
				inner join #SGS on t2.subjectGroupId=id and code='SG9'
			where testsession_id = tblTasStatsTestSessions.testsession_id) as varchar)
		else '' end as 'SG9'
  from tableUsers
      inner join tblUsersTargetGroups on UserCn = intUserCn
      inner join tblTasStatsTestSessions on user_id = intUserCn
  where tblTasStatsTestSessions.test_id = @test_id 
      and not exists(select * from tblTasStatsTestSessions t1 where t1.test_id = @test_id and t1.user_id = intUserCn and t1.endDate&gt;tblTasStatsTestSessions.endDate)
	  and tblUsersTargetGroups.targetGroup_id = @targetGroup_id

DROP TABLE #SGS
end
else
  select 
        Nachname as 'Name',
        Vorname as 'Vorname',
        descUserCn as 'Kennung',
        case when passed = 1 then 'Bestanden' else 'nich Bestanden' end as 'Ergebnis',
        pctQuestions as 'Prozentsatz gesamt',
		endDate as 'Datum'
  from tableUsers
      inner join tblUsersTargetGroups on UserCn = intUserCn
      inner join tblTasStatsTestSessions on user_id = intUserCn
  where tblTasStatsTestSessions.test_id = @test_id 
      and not exists(select * from tblTasStatsTestSessions t1 where t1.test_id = @test_id and t1.user_id = intUserCn and t1.endDate&gt;tblTasStatsTestSessions.endDate)
	  and tblUsersTargetGroups.targetGroup_id = @targetGroup_id

</command>
    </Report>
  </Reports>
  <Parameters>
    <Parameter id="4aaf4f45-f70a-452a-92b5-f8e9b8b0dfb6" isSystem="True" name="Test" reportParameterType_id="c613c165-96af-465b-bf6c-c0cba9a2dc5d" queryParameterName="@test_id" />
    <Parameter id="a5485539-67e0-4912-8255-8081a54efb3c" isSystem="True" name="Zielgruppe" reportParameterType_id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" queryParameterName="@targetGroup_id" />
  </Parameters>
  <ParameterTypes>
    <ParameterType id="c613c165-96af-465b-bf6c-c0cba9a2dc5d" isSystem="True" name="Test" datatype="Test" dataValueField="" dataTextField="" />
    <ParameterType id="9ae252e1-18b4-4b71-982c-e27b2d3a5287" isSystem="True" name="TargetGroup" datatype="TargetGroup" dataValueField="" dataTextField="" />
  </ParameterTypes>
</ReportsExport>