CREATE VIEW [C6MartPeriodico].[vPatrimonioBFAggregato_SenzaNobPrez] AS SELECT PATRIMONIO_BF.RETE, PATRIMONIO_BF.COD_FISCALE, SUM(CTV) as CTV, sum(case Upper(id_area) when 'NA' then ctv else 0 end ) as PatrimonioNonAssociato, sum(case Upper(id_area) when 'CC' then ctv else 0 end ) as ContoCorrente, --INIZIO INTERVENTI OMNIA sum(case Upper(id_area) when 'self' then ctv else 0 end ) as CTVSelf, -- sum(case Upper(id_area) when 'CC' then 0 else ctv end ) as PatrimonioSenzaCC, sum(case Upper(id_area) when 'CC' then 0 when 'Self' then 0 else ctv end ) as PatrimonioSenzaCC, --FINE INTERVENTI OMNIA --MAX((isnull(PART_VIAGG.PARTVIA_INV,0))) as PartiteViaggiantiInv, sum((isnull([C6MartPeriodico].PATRIMONIO_BF.PARTVIA_DISINV,0))) as PartiteViaggiantiDisinv FROM [C6MartPeriodico].PATRIMONIO_BF where isnull(nob_prez,'')<>'N' /* versione usata per l'estrazione della s80 per qc SELECT PATRIMONIO_BF.RETE, PATRIMONIO_BF.COD_FISCALE, SUM(CTV) as CTV, sum(case Upper(id_area) when 'NA' then ctv else 0 end ) as PatrimonioNonAssociato, sum(case when (Upper(id_area)='CC' and ctv<0 )then ctv else 0 end ) as ContoCorrente, sum(case when (Upper(id_area)='CC' and ctv<0) then 0 else ctv end ) as PatrimonioSenzaCC, --MAX((isnull(PART_VIAGG.PARTVIA_INV,0))) as PartiteViaggiantiInv, sum((isnull([C6MartPeriodico].PATRIMONIO_BF.PARTVIA_DISINV,0))) as PartiteViaggiantiDisinv FROM [C6MartPeriodico].PATRIMONIO_BF */ --atringali subquery non serve a nulla --LEFT JOIN --( -- SELECT a.rete, a.cod_fiscale, /*sum (a.PARTVIA_INV) AS PARTVIA_INV,*/ sum (a.PARTVIA_DISINV) AS PARTVIA_DISINV -- FROM ( -- SELECT DISTINCT rete, cod_fiscale, id_contratto, /*partvia_inv,*/ partvia_disinv -- FROM C6MartPeriodico.PATRIMONIO_bf -- --WHERE partvia_disinv IS NOT NULL -- ) a -- GROUP BY rete, cod_fiscale --) PART_VIAGG -- ON -- PART_VIAGG.RETE = PATRIMONIO_bf.RETE -- AND PART_VIAGG.COD_FISCALE = PATRIMONIO_bf.COD_FISCALE GROUP BY PATRIMONIO_BF.RETE, PATRIMONIO_BF.COD_FISCALE