PDC_REPORT_CreazioneDB/sql/Testbes/procedure/C6Mart_PL_S1Patrimonio.sql
2025-06-10 15:29:40 +02:00

101 lines
4.6 KiB
Transact-SQL

-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
--[C6Mart].PL_S1Patrimonio 'F','BRTVTR64T13L219G'
CREATE procedure [C6Mart].[PL_S1Patrimonio]
-- 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
DECLARE @PatrimonioBF DECIMAL(15,2)
DECLARE @PatrimonioTerzi DECIMAL(15,2)
DECLARE @PatrimonioImmobiliare DECIMAL(15,2)
DECLARE @CCBF DECIMAL(15,2)
DECLARE @PatrimonioAltro DECIMAL(15,2)
DECLARE @PatrimonioTotale DECIMAL(15,2)
DECLARE @Part_viaggianti decimal(15,2)
SELECT @PatrimonioBF=SUM(CTV)
FROM C6Mart.PATRIMONIO_BF patrBF
WHERE
patrBF.Rete = @Rete
and patrBF.Cod_Fiscale = @CodiceFiscale
and patrBF.id_area <> 'CC'
--select '@PaRT_VIAGGIANTI'
SELECT @Part_viaggianti=SUM(IMPORTO)
from
(
SELECT SUM(IMPORTO) AS IMPORTO
FROM C6Mart.PARTITE_VIAGGIANTI partvia
WHERE
partvia.Rete = @Rete
and partvia.Cod_Fiscale = @CodiceFiscale
union
SELECT SUM(PARTVIA_DISINV) AS IMPORTO
FROM C6Mart.patrimonio_bf partvia
WHERE
partvia.Rete = @Rete
and partvia.Cod_Fiscale = @CodiceFiscale
) H
--select '@PatrimonioBF',@PatrimonioBF
SELECT @PatrimonioTerzi=SUM(ROUND(CTV,2))
FROM C6Mart.PATRIMONIO_TERZI patrTerzi
WHERE
patrTerzi.Rete = @Rete
and patrTerzi.Cod_Fiscale = @CodiceFiscale
--select '@PatrimonioTerzi',@PatrimonioTerzi
SELECT @PatrimonioImmobiliare=SUM(patrImm.VALORE_STIMATO)
FROM C6Mart.PATRIMONIO_IMMOBILIARE patrImm
WHERE
patrImm.Rete = @Rete
and patrImm.Cod_Fiscale = @CodiceFiscale
--select '@PatrimonioImmobiliare',@PatrimonioImmobiliare
-- SELECT @CCBF=SUM(ccBB.saldo)
-- FROM C6Mart.ANAG_CC ccBB
-- WHERE
-- ccBB.Rete = @Rete
-- and ccBB.Cod_Fiscale = @CodiceFiscale
--select '@CCBF',@CCBF
SELECT @PatrimonioAltro=SUM(patrAltro.ctv)
FROM C6Mart.PATRIMONIO_ALTRO patrAltro
WHERE
patrAltro.Rete = @Rete
and patrAltro.Cod_Fiscale = @CodiceFiscale
--select '@PatrimonioAltro',@PatrimonioAltro
declare @summa as decimal(15,2)
SET @Part_viaggianti=coalesce(@Part_viaggianti,0)
SET @PatrimonioBF =coalesce(@PatrimonioBF,0)
SET @summa = @PatrimonioBF+ @Part_viaggianti
SET @PatrimonioBF=@summa
SET @PatrimonioTerzi =coalesce(@PatrimonioTerzi,0)
SET @PatrimonioImmobiliare =coalesce(@PatrimonioImmobiliare,0)
--SET @CCBF = coalesce(@CCBF,0)
SET @PatrimonioAltro= coalesce(@PatrimonioAltro,0)
-- SET @PatrimonioTotale = coalesce(@PatrimonioBF+@PatrimonioTerzi+@PatrimonioImmobiliare+@PatrimonioAltro+@CCBF,0)
SET @PatrimonioTotale = coalesce(@PatrimonioBF+@PatrimonioTerzi+@PatrimonioImmobiliare+@PatrimonioAltro,0)
--select '@PatrimonioTotale',@PatrimonioTotale
-- SELECT @PatrimonioBF + @CCBF as BFCTV,
-- @PatrimonioTerzi as PatrimonioTerziCTV,
-- @PatrimonioBF + @CCBF +@PatrimonioTerzi as FinanziarioCTV,
-- cast((case @PatrimonioTotale when 0.00 then 0.00 else (@PatrimonioBF + @CCBF + @PatrimonioTerzi)/ @PatrimonioTotale end) as Decimal(15,2)) * 100.00 as FinanziarioPerc,
-- @PatrimonioImmobiliare as ImmobiliareCTV,
-- cast(case @PatrimonioTotale when 0.00 then 0.00 else @PatrimonioImmobiliare/@PatrimonioTotale end as Decimal(15,2)) *100.00 as ImmobiliarePerc,
-- @PatrimonioAltro as AltroCTV,
-- cast(case @PatrimonioTotale when 0.00 then 0.00 else @PatrimonioAltro/@PatrimonioTotale end as Decimal(15,2)) *100.00 as AltroPerc,
-- @PatrimonioTotale as TotaleCTV
SELECT @PatrimonioBF as BFCTV,
@PatrimonioTerzi as PatrimonioTerziCTV,
@PatrimonioBF + @PatrimonioTerzi as FinanziarioCTV,
--cast((case @PatrimonioTotale when 0.00 then 0.00 else (@PatrimonioBF + @PatrimonioTerzi)/ @PatrimonioTotale end) as Decimal(15,2)) * 100.00 as FinanziarioPerc,
convert(decimal(6,2),((case @PatrimonioTotale when 0.00 then 0.00 else (@PatrimonioBF + @PatrimonioTerzi)/ @PatrimonioTotale end) * 100.00)) as FinanziarioPerc,
@PatrimonioImmobiliare as ImmobiliareCTV,
--cast(case @PatrimonioTotale when 0.00 then 0.00 else @PatrimonioImmobiliare/@PatrimonioTotale end as Decimal(15,2)) *100.00 as ImmobiliarePerc,
convert(decimal(6,2),((case @PatrimonioTotale when 0.00 then 0.00 else @PatrimonioImmobiliare/@PatrimonioTotale end ) *100.00)) as ImmobiliarePerc,
@PatrimonioAltro as AltroCTV,
--cast(case @PatrimonioTotale when 0.00 then 0.00 else @PatrimonioAltro/@PatrimonioTotale end as Decimal(15,2)) *100.00 as AltroPerc,
convert(decimal(6,2),(case @PatrimonioTotale when 0.00 then 0.00 else @PatrimonioAltro/@PatrimonioTotale end) *100.00) as AltroPerc,
@PatrimonioTotale as TotaleCTV
END