SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO DROP PROCEDURE IF EXISTS uspGetOsztalyokHaviHianyzasiOsszesitoje_honapra GO CREATE PROCEDURE uspGetOsztalyokHaviHianyzasiOsszesitoje_honapra @tanevID int, @intezmenyId int, @honap int AS BEGIN --Összes osztály create table #osztaly (Id int) insert into #osztaly select ocs.id as Id from t_osztalycsoport ocs inner join t_osztaly o on o.id=ocs.id where ocs.torolt='F' and ocs.c_tanevid=@tanevid and ocs.c_intezmenyid=@intezmenyid --Igazolások és típusai create table #igazolas (Osztaly int, IgazoltE nvarchar(1), IgazolasTipus int) insert into #igazolas select tao.c_osztalycsoportid as Osztaly, tm.c_igazolt as IgazoltE, tm.c_igazolastipusa as IgazolasTipus from t_tanulomulasztas tm inner join t_tanitasiora tao on tao.id=tm.c_tanitasiorakid and tao.torolt='F' and tao.c_tanevid=@tanevid and tao.c_intezmenyid=@intezmenyid and (CAST(MONTH(tao.c_datum) AS VARCHAR))=@honap where tm.torolt='F' and tm.c_intezmenyid=@intezmenyid and tm.c_tanevid=@tanevid --Mulasztások create table #mulasztas (Nap int, Tanulo int, Osztaly int) insert into #mulasztas select CAST(DAY(tao.c_datum) as int) as Nap, tm.c_oratanuloiid as Tanulo, tao.c_osztalycsoportid as Osztaly from t_tanulomulasztas tm inner join t_tanitasiora tao on tao.id=tm.c_tanitasiorakid and tao.torolt='F' and tao.c_tanevid=@tanevid and tao.c_intezmenyid=@intezmenyid and (CAST(MONTH(tao.c_datum) AS VARCHAR))=@honap where tm.torolt='F' and tm.c_intezmenyid=@intezmenyid and tm.c_tanevid=@tanevid select ocs.c_nev as COLUMN379, iif(m.[1]=0, null, m.[1]) as [1], iif(m.[2]=0, null, m.[2]) as [2], iif(m.[3]=0, null, m.[3]) as [3], iif(m.[4]=0, null, m.[4]) as [4], iif(m.[5]=0, null, m.[5]) as [5], iif(m.[6]=0, null, m.[6]) as [6], iif(m.[7]=0, null, m.[7]) as [7], iif(m.[8]=0, null, m.[8]) as [8], iif(m.[9]=0, null, m.[9]) as [9], iif(m.[10]=0, null, m.[10]) as [10], iif(m.[11]=0, null, m.[11]) as [11], iif(m.[12]=0, null, m.[12]) as [12], iif(m.[13]=0, null, m.[13]) as [13], iif(m.[14]=0, null, m.[14]) as [14], iif(m.[15]=0, null, m.[15]) as [15], iif(m.[16]=0, null, m.[16]) as [16], iif(m.[17]=0, null, m.[17]) as [17], iif(m.[18]=0, null, m.[18]) as [18], iif(m.[19]=0, null, m.[19]) as [19], iif(m.[20]=0, null, m.[20]) as [20], iif(m.[21]=0, null, m.[21]) as [21], iif(m.[22]=0, null, m.[22]) as [22], iif(m.[23]=0, null, m.[23]) as [23], iif(m.[24]=0, null, m.[24]) as [24], iif(m.[25]=0, null, m.[25]) as [25], iif(m.[26]=0, null, m.[26]) as [26], iif(m.[27]=0, null, m.[27]) as [27], iif(m.[28]=0, null, m.[28]) as [28], iif(m.[29]=0, null, m.[29]) as [29], iif(m.[30]=0, null, m.[30]) as [30], iif(m.[31]=0, null, m.[31]) as [31] , Osszes.Osszes as COLUMN2672, Igazolt.Igazolt as COLUMN2568, Egyeb.Egyeb as COLUMN5197, Orvosi.Orvosi as COLUMN1786, Szuloi.Szuloi as COLUMN1787, Hivatalos.Hivatalos as COLUMN1788, IskolaErdeku.IskolaErdeku as COLUMN1789, Igazolatlan.Igazolatlan as COLUMN1911 from #osztaly o left join (select * from #mulasztas m pivot (count(Tanulo) for Nap in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14],[15],[16],[17],[18],[19],[20],[21],[22],[23],[24],[25],[26],[27],[28],[29],[30],[31]))pv) m on m.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Osszes from #igazolas group by Osztaly) Osszes on Osszes.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Igazolt from #igazolas i where i.IgazoltE='T' group by Osztaly) Igazolt on Igazolt.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Igazolatlan from #igazolas i where i.IgazoltE='F' group by Osztaly) Igazolatlan on Igazolatlan.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Egyeb from #igazolas i where i.IgazolasTipus=1529 group by Osztaly) Egyeb on Egyeb.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Orvosi from #igazolas i where i.IgazolasTipus=1530 group by Osztaly) Orvosi on Orvosi.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Szuloi from #igazolas i where i.IgazolasTipus=1531 group by Osztaly) Szuloi on Szuloi.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as Hivatalos from #igazolas i where i.IgazolasTipus=1532 group by Osztaly) Hivatalos on Hivatalos.Osztaly=o.Id left join (select Osztaly, count(Osztaly) as IskolaErdeku from #igazolas i where i.IgazolasTipus=1533 group by Osztaly) IskolaErdeku on IskolaErdeku.Osztaly=o.Id inner join t_osztalycsoport ocs on ocs.id=o.Id order by ocs.c_nev drop table #mulasztas drop table #osztaly drop table #igazolas END GO