-- =============================================
-- Author:		<Author,,Name>
-- Create date: <Create Date,,>
-- Description:	<Description,,>
-- =============================================
-- [C6Mart].[PL_S31PatrimonioFinanziarioIntermediario]  'F','BRTVTR64T13L219G'
CREATE procedure [C6Mart].[PL_S31PatrimonioFinanziarioIntermediario] 
	-- Add the parameters for the stored procedure here
	-- Add the parameters for the stored procedure here
	@Rete char(1), 
	@CodiceFiscale varchar(16)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SELECT DISTINCT
		PATRIMONIO_TERZI.INTERMEDIARIO as Intermediario,
		SUM(ROUND(PATRIMONIO_TERZI.CTV,2)) OVER (PARTITION BY PATRIMONIO_TERZI.INTERMEDIARIO) as Controvalore,
		(SUM(PATRIMONIO_TERZI.CTV) OVER (PARTITION BY PATRIMONIO_TERZI.INTERMEDIARIO)/
		SUM(PATRIMONIO_TERZI.CTV) OVER (PARTITION BY 'Totale'))*100 as Percentuale,
		SUM(PATRIMONIO_TERZI.CTV) OVER (PARTITION BY 'Totale') as Totale
	FROM C6MART.PATRIMONIO_TERZI
	WHERE PATRIMONIO_TERZI.RETE = @Rete
		AND PATRIMONIO_TERZI.COD_FISCALE = @CodiceFiscale
	ORDER BY PATRIMONIO_TERZI.INTERMEDIARIO 
END