﻿-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date, ,>
-- Description:	<Description, ,>
-- =============================================
CREATE FUNCTION [C6MartPeriodico].[GetPercentualePiramideMod] 
(
	-- Add the parameters for the function here
	@CodFis as varchar(20),
	@rete as varchar(1),
	@area as varchar(5)
)
RETURNS decimal(10,2)
AS
BEGIN
	-- Declare the return variable here
	DECLARE @perc as decimal(10,2)
	--DECLARE @perc2 as decimal(10,2)
	-- Add the T-SQL statements to compute the return value here
	--IF @area = 'Pre'
	--begin
	--SELECT @perc = Sum(pmod.Perc_Area)
	--FROM [C6StampeCentralizzate].[C6MartPeriodico].[PIRAMIDE_MOD] pmod
	--inner join [C6StampeCentralizzate].[C6MartPeriodico].Area_Bisogno ar on pmod.id_area=ar.Id_area
	--where pmod.COD_FISCALE = @CodFis
	--and pmod.rete = @rete
	--and ar.Id_area = @area
	----and ar.Id_area in ('Liq','Ris','Pre','Pre1','Inv','Ext')
	--group by ar.Id_area,ar.Nome_area
	--SELECT @perc2 = Sum(pmod.Perc_Area)
	--FROM [C6StampeCentralizzate].[C6MartPeriodico].[PIRAMIDE_MOD] pmod
	--inner join [C6StampeCentralizzate].[C6MartPeriodico].Area_Bisogno ar on pmod.id_area=ar.Id_area
	--where pmod.COD_FISCALE = @CodFis
	--and pmod.rete = @rete
	--and ar.Id_area = 'Pre1'
	----and ar.Id_area in ('Liq','Ris','Pre','Pre1','Inv','Ext')
	--group by ar.Id_area,ar.Nome_area
	--Set @perc = @perc + @perc2;
	--end;
	--Else
	--begin
	SELECT @perc = Sum(pmod.Perc_Area)
	FROM [C6StampeCentralizzate].[C6MartPeriodico].[PIRAMIDE_MOD] pmod
	inner join [C6StampeCentralizzate].[C6MartPeriodico].Area_Bisogno ar on pmod.id_area=ar.Id_area
	where pmod.COD_FISCALE = @CodFis
	and pmod.rete = @rete
	and ar.Id_area = @area
	--and ar.Id_area in ('Liq','Ris','Pre','Pre1','Inv','Ext')
	group by ar.Id_area,ar.Nome_area
	--end;
	-- Return the result of the function
	RETURN @perc
END