set xact_abort on; go begin transaction; go set ANSI_NULLS on; go /********************************************************** * STORED stp_freqProjByDipPeriodo * * elenco dei progetti/fasi per dipendente/periodo * * mod: S.E.L. 2013.02.20 * **********************************************************/ alter PROCEDURE stp_freqProjByDipPeriodo ( @idxDipendente INT = 0, -- 0 = tutti @inizio DATETIME, @fine DATETIME, @maxRes INT = 999 -- num max risultati desiderati ) AS -- calcolo il totale dei risultati per prima cosa... DECLARE @totNum AS INT = 1 DECLARE @tabFreq AS TABLE ( nomeProj NVARCHAR(250) ,nomeFase NVARCHAR(250) ,qty DECIMAL(9,3) ,idxFase INT ,idxFaseComm INT ) INSERT @tabFreq SELECT TOP (@maxRes) nomeProj, nomeFase ,SUM(ra.oreTot) as qty, ra.idxFase, af.idxFaseAncest FROM RegAttivita ra INNER JOIN AnagFasi af ON ra.idxFase=af.idxFase INNER JOIN AnagProgetti ap ON af.idxProgetto=ap.idxProgetto WHERE ra.inizio BETWEEN @inizio AND @fine AND (ra.idxDipendente = @idxDipendente OR @idxDipendente = 0 ) GROUP BY nomeProj, nomeFase, ra.idxFase, af.idxFaseAncest ORDER BY qty DESC -- calcolo il totale SELECT @totNum = ISNULL(SUM(qty),1) FROM @tabFreq ;WITH myCTE AS ( SELECT idxFaseComm, idxFase, nomeProj, nomeFase, qty/@totNum as freq, qty, @totNum AS tot FROM @tabFreq ) -- restituisco tab con calcolo freq reale SELECT af.nomeFase as nomeComm, tf.* FROM myCTE tf INNER JOIN AnagFasi af ON tf.idxFaseComm = af.idxFase RETURN go commit; go -- registro versione... INSERT INTO [dbo].[LogUpdateDb] ([Versione],[Data]) VALUES(359, GETDATE()) GO SELECT * FROM LogUpdateDb ORDER BY Versione DESC