SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

IF OBJECT_ID('[sp_KIRSTAT_a02t10]') IS NOT NULL
BEGIN
  DROP PROCEDURE [sp_KIRSTAT_a02t10]
END
GO

CREATE PROCEDURE [dbo].[sp_KIRSTAT_a02t10]
  @tanevId int,
  @intezmenyId int
AS
BEGIN
  SET NOCOUNT ON;

select d.c_name as Column4962, count(TanarokSzama) as Column4958, isnull(nok.Nok,0) as Column4959, isnull(nyug.nyug,0) as Column4960,
isnull(kepesitetlen.Kepesitetlen,0) as 'Column4961'
from

--Tanárok száma
(select distinct t.c_targykategoria as Targy, ft.c_tanarokid as TanarokSzama from t_foglalkozasok_tanarok ft
inner join t_foglalkozas fo on fo.id=ft.c_foglalkozasokid and fo.c_tanevid=@tanevid and fo.c_intezmenyid=@intezmenyid
inner join t_tantargy t on t.id=fo.c_tantargyid where fo.torolt='F' and t.torolt='F' and t.c_tanevid=@tanevid and t.c_intezmenyid=@intezmenyid) asd
inner join (select distinct id, c_name from t_dictionaryitembase d where d.torolt='F' and d.c_tanevid=@tanevid and d.c_intezmenyid=@intezmenyid) d on d.id=Targy 

--Nok
left join (select d.c_name as Targy, count(TanarokSzama) as Nok from
(select distinct t.c_targykategoria as Targy, ft.c_tanarokid as TanarokSzama from t_foglalkozasok_tanarok ft
inner join t_foglalkozas fo on fo.id=ft.c_foglalkozasokid and fo.torolt='F' and fo.c_tanevid=@tanevid and fo.c_intezmenyid=@intezmenyid
inner join t_tantargy t on t.id=fo.c_tantargyid and t.torolt='F' and t.c_tanevid=@tanevid and t.c_intezmenyid=@intezmenyid) asd
inner join (select distinct id, c_name from t_dictionaryitembase d where d.torolt='F' and d.c_tanevid=@tanevid and d.c_intezmenyid=@intezmenyid) d on d.id=Targy
inner join t_felhasznalo fel on fel.id=asd.TanarokSzama and fel.torolt='F' and fel.c_tanevid=@tanevid and fel.c_intezmenyid=@intezmenyid
where fel.c_neme=5 
group by d.c_name) as nok on nok.Targy=d.c_name

--Nyugdijasok
left join (select d.c_name as Targy, count(TanarokSzama) as Nyug from
(select distinct t.c_targykategoria as Targy, ft.c_tanarokid as TanarokSzama from t_foglalkozasok_tanarok ft
inner join t_foglalkozas fo on fo.id=ft.c_foglalkozasokid and fo.torolt='F' and fo.c_tanevid=@tanevid and fo.c_intezmenyid=@intezmenyid
inner join t_tantargy t on t.id=fo.c_tantargyid and t.torolt='F' and t.c_tanevid=@tanevid and t.c_intezmenyid=@intezmenyid
inner join t_munkaugyiadatok m on m.c_alkalmazottid=ft.c_tanarokid and m.c_nyugdijas='T' and m.torolt='F' and m.c_tanevid=@tanevid and m.c_intezmenyid=@intezmenyid
) asd
inner join (select distinct id, c_name from t_dictionaryitembase d where d.c_tanevid=@tanevid and d.c_intezmenyid=@intezmenyid) d on d.id=Targy
inner join t_felhasznalo fel on fel.id=asd.TanarokSzama and fel.torolt='F' and fel.c_tanevid=@tanevid and fel.c_intezmenyid=@intezmenyid
group by d.c_name) as nyug on nyug.Targy=d.c_name

--Képesítettek
left join (select Targy, count(Alkalmazott) as Kepesitetlen from
(select distinct d.c_name as Targy, c_alkalmazottid as Alkalmazott from t_munkaugyiadatok m
inner join t_foglalkozasok_tanarok ft on ft.c_tanarokid=m.c_alkalmazottid 
inner join t_foglalkozas f on f.id=ft.c_foglalkozasokid and f.torolt='F' and f.c_tanevid=@tanevid and f.c_intezmenyid=@intezmenyid
inner join t_tantargy t on t.id=f.c_tantargyid and t.torolt='F' and t.c_tanevid=@tanevid and t.c_intezmenyid=@intezmenyid
inner join (select distinct id, c_name from t_dictionaryitembase d where d.torolt='F' and d.c_tanevid=@tanevid and d.c_intezmenyid=@intezmenyid) d on d.id=t.c_targykategoria
where C_BESOROLASIFOKOZAT=3040 or C_BESOROLASIFOKOZAT is null
)tabla
group by Targy) as kepesitetlen on kepesitetlen.Targy=d.c_name

group by d.id,d.c_name, nok.Nok, nyug.Nyug, kepesitetlen.Kepesitetlen
order by Column4962


END



