How to add sql values in column

Asked

Viewed 720 times

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 him give me the result of the sum of the column Patsaldobem, together with the highest value of PatSaldoDepAcum?

Follow the image: inserir a descrição da imagem aqui

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

  • I suggest that, considering the data in the figure, you inform us how the result should be. For example: (1) what to do with the columns PatSldBemValResidAnt and PatSldBemValResid? (2) the values shall be grouped by the column PatBemCodRed?

1 answer

2


Try it like this

 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 existing in the column, if you want the lowest value you can use the MIN(campo).

  • yes but I need to add tbm to the column Patsaldobemvaldep has to give 544.11 the total

  • Right, but in my answer I informed the camp there ,Sum(PS.PatSidBemValDep) PatSldBemValDep... Are you saying my answer is wrong or just missing the field?

  • I adjusted the sql, I had put the name of Fields wrong.

  • He had to add the values 181.37 the 3

  • So tell me... Are you adding wrong? Aren’t you adding up? What’s wrong with my example?

  • It’s not wrong. It only has to add up the 3 values 181.37

  • 181.37+181.37+181.37

  • Dude, edit your question then and explain correctly what you want. The example I gave you is to result something +/- like this PatSldBemValDepAcum: 21492,55 and PatSldBemValDep = 544,11, according to the image you gave us. Also explain what is the result you want and which fields should be results.

  • Patsldbemvaldep = 544,11 did not give this result

  • I’ve already edited the question

  • worked out just by taking out the group by. vlw by the help

  • Whoa, good guy, just pay more attention when analyzing the scripts!

Show 7 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.