use of Sum SQL SERVER

1

I need to add everything that is in value;

This is my query

create procedure sp_DetalleDeduccionesEmpleado @inicio datetime, @final datetime, @emp int
as
select 
ded.DedId,
sum(ded.DetDedEmpValor),
emp.EmpId


from Tbl_DetalleDeduccionesEmpleado ded
inner join Tbl_Deducciones tip
 on tip.DedId = ded.DedId
 inner join Tbl_Empleado emp
 on emp.EmpId = ded.EmpId
 where ded.DetDedEmpFecha >=  @inicio and 
 ded.DetDedEmpFecha <= @final and 
 ded.EmpId = @emp
 group by ded.DedId, emp.EmpId



execute sp_DetalleDeduccionesEmpleado '2018-05-01', '2018-05-08', '2390'
    
asked by Marco Eufragio 27.10.2018 в 00:13
source

1 answer

4

To do this, you must embed your initial selection, within a greater selection in the following way:

SELECT SUM(X.SumValor)  -- Sumas la columna especificada
FROM(
    select 
        ded.DedId,
        sum(ded.DetDedEmpValor) AS 'SumValor',  -- Aquí debes ponerle un alias
        emp.EmpId
    from Tbl_DetalleDeduccionesEmpleado ded
    inner join Tbl_Deducciones tip
        on tip.DedId = ded.DedId
    inner join Tbl_Empleado emp
        on emp.EmpId = ded.EmpId
    where ded.DetDedEmpFecha >=  @inicio and 
        ded.DetDedEmpFecha <= @final and 
        ded.EmpId = @emp
    group by ded.DedId, emp.EmpId
) AS X

NOTE: You can also use a CTE for this type of situation. Greetings.

    
answered by 27.10.2018 / 00:19
source