SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF OBJECT_ID('[dbo].[sp_GetStatTanuloTantargyiMulasztasMegtartott]') IS NOT NULL BEGIN DROP PROCEDURE [dbo].[sp_GetStatTanuloTantargyiMulasztasMegtartott] END GO CREATE PROCEDURE [dbo].[sp_GetStatTanuloTantargyiMulasztasMegtartott] @osztalyId int, @TanevId int AS BEGIN SET NOCOUNT ON; create table #students (Id int) insert into #students select f.id from T_TANULOCSOPORT_OSSZES tcs inner join T_FELHASZNALO_OSSZES f on f.id=tcs.C_TANULOID where tcs.c_osztalycsoportid=@osztalyid and tcs.torolt='F' and tcs.C_KILEPESDATUM is null order by f.C_NYOMTATASINEV create table #mulasztas (Ossz int, Tanulo int, Tantargy int, OsztalyCsoport int) insert into #mulasztas select count(tm.id), tm.c_oratanuloiid, t.id, tao.c_osztalycsoportid from T_TANULOMULASZTAS_OSSZES tm inner join T_TANITASIORA_OSSZES tao on tao.id=tm.c_tanitasiorakid inner join T_TANTARGY_OSSZES t on t.id=tao.C_TANTARGYID inner join #students s on s.id=tm.C_ORATANULOIID where tm.c_tanevId=@tanevId and tm.Torolt='F' and tm.C_TIPUS=1500--and tm.c_igazolt='T' or tm.c_igazolt='F' ????? and ((tao.C_OSZTALYCSOPORTID in (select id from t_osztaly_osszes)) or (tao.c_osztalycsoportid in (select id from t_csoport_osszes where C_TIPUSA=1034))) group by tm.c_oratanuloiid, t.id, tao.c_osztalycsoportid create table #naplozott (Tanulo int, Tantargy int, OsztalyCsoport int, Ossz int) insert into #naplozott select m.Tanulo, m.Tantargy, m.OsztalyCsoport, count(distinct tao.id) from #mulasztas m inner join T_TANITASIORA_OSSZES tao on tao.C_OSZTALYCSOPORTID=m.OsztalyCsoport and tao.C_TANTARGYID=m.Tantargy and tao.C_MEGTARTOTT='T' inner join T_TANULOCSOPORT_OSSZES tcs on tcs.C_TANULOID=m.Tanulo and tcs.C_OSZTALYCSOPORTID=m.OsztalyCsoport and ((tao.C_OSZTALYCSOPORTID in (select id from t_osztaly_osszes)) or (tao.c_osztalycsoportid in (select id from t_csoport_osszes where C_TIPUSA=1034))) where tao.TOROLT='F' and tao.C_DATUM>=tcs.C_BELEPESDATUM group by m.Tanulo, m.Tantargy, m.OsztalyCsoport declare @cols as nvarchar(max), @query as nvarchar(max), @colsIn as nvarchar(max) select @cols= isnull(@cols + ',','') +quotename(Tantargy) from (select distinct m.Tantargy as Tantargy from #mulasztas m) Tantargy select @colsIn= isnull(@colsIn + ',','') +'convert(nvarchar(max),'+quotename(Tantargy) + ')+''%'' as '+quotename(nev) from (select distinct m.Tantargy as Tantargy, t.c_nev nev from #mulasztas m inner join T_TANTARGY_OSSZES t on t.id=m.Tantargy) TantargyCols set @query=' select fel.c_nyomtatasinev Tanulo, '+@colsIn+' from (select mulasztas.Tanulo, round(convert(float, mulasztas.Ossz)/convert(float, naplozott.Ossz)*100, 0) Osszes, mulasztas.Tantargy from #students s inner join #mulasztas mulasztas on mulasztas.Tanulo=s.id inner join #naplozott naplozott on naplozott.Tantargy=mulasztas.Tantargy and naplozott.OsztalyCsoport=mulasztas.OsztalyCsoport and naplozott.Tanulo=mulasztas.Tanulo)a pivot (max(Osszes) for Tantargy in ('+@cols+'))pv inner join t_felhasznalo_osszes fel on fel.id=Tanulo order by fel.c_nyomtatasinev ' EXEC sp_executesql @query drop table #students drop table #mulasztas drop table #naplozott END GO