How to sum values in sql in column

1

I have the following sql code:

 select PS.PatSldBemValResidAnt
       ,PS.PatSldBemValDepAcum 
       ,Sum(PS.PatSldBemValDep)   PatSldBemValDep
       ,Sum(PS.PatSldBemValResid) PatSldBemValResid       
  from  Pat_Saldo_Bem as PS With(Nolock)
  where PS.EmpCod          LIKE '%01.14%'
    and PS.PatBemCodRed       = '0000351'
    and ((PS.PatSldBemAnoMes >= '201807')
    and (PS.PatSldBemAnoMes  <= '201810'))
 group by PS.PatSldBemValResidAnt, PS.PatSldBemValDepAcum, PS.PatSldBemValDep

How do I make it give me the result of the sum of the column Patsaldobem , along with the highest value of PatSaldoDepAcum ?

Follow the image:

I would like to show the column total PatSaldoBemValDep which is the sum of the values 187.37+187.37+187.37

    
asked by anonymous 19.10.2018 / 16:55

1 answer

2

Try this way

 Select Max(PS.PatSldBemValDepAcum)
       ,Sum(PS.PatSldBemValDep) PatSldBemValDep
  From  Pat_Saldo_Bem  as PS With(Nolock)
  Where PS.EmpCod LIKE '%01.14%'
    And PS.PatBemCodRed = '0000351'
    And ((PS.PatSldBemAnoMes >= '201807')
    And (PS.PatSldBemAnoMes <= '201810'))

The MAX(campo) will result in the highest value in the column, if you want the lowest value you can use MIN(campo) .

    
19.10.2018 / 17:01