-- ============================================= -- Description: Rudlof Kristóf - Tanár órarendi lefedettség -- ============================================= SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO IF OBJECT_ID('[dbo].[sp_GetTanarOrarendLefedettseg]') IS NOT NULL BEGIN DROP PROCEDURE [dbo].[sp_GetTanarOrarendLefedettseg] END GO CREATE PROCEDURE [dbo].[sp_GetTanarOrarendLefedettseg] @intezmenyId int, @tanevId int AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from interfering with SELECT statements. SET NOCOUNT ON; declare @AktivtanevId int = (select Id from T_TANEV_OSSZES tanev where c_aktiv='T' and c_intezmenyId=@intezmenyId and Torolt='F') declare @HetEleje date declare @HetVege date if (@tanevId=@AktivtanevId) begin set @HetEleje =(select distinct nh.c_hetkezdonapja from T_NAPTARIHET_OSSZES nh where getdate()>nh.c_hetkezdonapja and getdate()nh.c_hetkezdonapja and getdate()nh.c_hetkezdonapja and @UtolsoNapnh.c_hetkezdonapja and @UtolsoNap=@HetEleje) or (oo.c_oraervenyessegkezdete<=@HetVege and oo.c_oraervenyessegvege>=@HetEleje)) and oo.c_tanevid=@tanevid and oo.torolt='F' inner join T_FOGLALKOZASOK_TANAROK_OSSZES ft on ft.c_foglalkozasokid=fog.id inner join T_FELHASZNALO_OSSZES f on f.id=ft.c_tanarokid and f.c_intezmenyid=@intezmenyId and f.c_tanevid=@tanevid and f.torolt='F' inner join (select distinct d.id, d.c_name from T_DICTIONARYITEMBASE_OSSZES d)d on d.id=cs.c_tipusa inner join (select distinct d.id, d.c_name from T_DICTIONARYITEMBASE_OSSZES d)Hetirend on Hetirend.id=oo.c_hetirend where cs.torolt='F' and cs.c_alintezmenyid=@intezmenyId and cs.c_altanevid=@tanevId select Pedagogus COLUMN456, CsoportTipus COLUMN444, sum(Oraszam) COLUMN3670 from #csoportok cs group by Pedagogus, CsoportTipus order by Pedagogus --órarendi lefedettség hetirenden declare @cols as nvarchar(max), @query as nvarchar(max); select distinct @cols= isnull(@cols + ',','') +quotename(Hetirend) from #csoportok set @query='select Pedagogus as COLUMN456, CsoportTipus COLUMN444, '+@cols+' from #csoportok cs pivot (sum(Oraszam) for Hetirend in ('+@cols+')) pv' EXEC sp_executesql @query; drop table #csoportok END GO