Add results of a SUM in SQL Server

0

I need to add a series of elements that are in fact the result of a sum, I will try to explain myself as best as possible.

I have the following query:

select distinct 
cp.descripcion,
cp.idSysCPPuestos,
 em.nombreLargo
,sum(CAST(em.sueldoDiario AS DECIMAL(18, 4)))+sum(CAST(em.sueldoIntegrado AS DECIMAL(18, 4))) [Total]


 from 
sysCPEmpleados em 
inner join sysCPCentroCosto cc on
em.idSysCPCentroCosto = cc.idSysCPCentroCosto
inner join sysCPPuestos cp on
em.idSysCPPuestos = cp.idSysCPPuestos

where cc.idSysCPCentroCosto = 'SBIB15C'



 group by 
    cp.descripcion,
    cp.idSysCPPuestos,
    em.nombreLargo
    ,em.idSysCPCentroCosto

Return the following:

The point is that I need to get the total data by its idSysCPPuestos , that is the sum of all those total is to get something like:

  

AFANDORA | GB022 | x.xx

     

ASSISTANT | GB025 | x.xx

Avoid repeating that I only showed the total of the sums "suamdas entre susi".

    
asked by E.Rawrdríguez.Ophanim 23.10.2018 в 23:28
source

1 answer

2

For this, what you need is to support yourself in a greater selection of the same selection that you have made, in which you would group as you need, adding up at the end. As follows:

select x.descripcion, x.idSysCPPuestos, SUM(Total)
from(
    select cp.descripcion,
        cp.idSysCPPuestos,
        em.nombreLargo, 
        sum(CAST(em.sueldoDiario AS DECIMAL(18, 4))) + sum(CAST(em.sueldoIntegrado AS DECIMAL(18, 4))) [Total]
    from sysCPEmpleados em 
    inner join sysCPCentroCosto cc 
        on em.idSysCPCentroCosto = cc.idSysCPCentroCosto
    inner join sysCPPuestos cp 
        on em.idSysCPPuestos = cp.idSysCPPuestos
    where cc.idSysCPCentroCosto = 'SBIB15C'
    group by 
        cp.descripcion,
        cp.idSysCPPuestos,
        em.nombreLargo
        ,em.idSysCPCentroCosto
) as x
group by x.descripcion, x.idSysCPPuestos

I hope your help has helped me. Greetings.

NOTE: If you already aggregate your SELECT under the same fields, the distinct is at that level too.

    
answered by 23.10.2018 / 23:40
source