How to take the sum of an entire column in SQLSERVER

Asked

Viewed 2,225 times

1

I need to take the total value of the VOL field (image below), it is already generated by the SUM function. It can be in another query. All periodic tithes are required.

ex: total = VOL(7,6890028) -> sum of the 4 rows in column VOL

inserir a descrição da imagem aqui

  • It is not clear your doubt, need to detail more

  • 1

    Takes the GROUP BY. If you want to add them all up, there’s no reason to discriminate Volumes.

  • I want the sum of all rows in column VOL

  • But you’re grouping by Volumes, it is illogical what you are saying. You want to add all, and repeat in the field VOL, and grouped by Volumes. Is your concept wrong, or is not clear the doubt.

  • Try GROUP BY CUBE (NF.VOLUMES)

  • 1

    @Motta even with CUBE or ROLLUP, I don’t think that was her idea. But, I’ll wait for more details.

  • first I list Vol of each invoice(each row is a note). But now I need the overall total (sum of volumes of all notes). Can be in another query.

  • @Motta - it worked... That’s right... Thank you "Try GROUP BY CUBE (NF.VOLUMES)"

  • @Motta put in the answer I mark as the right one

Show 4 more comments

2 answers

4


Something like :

Select campo1,
       sum(valor1) valor
from tabela
group by cube (campo1)
  • Cube () --- that there

  • For other useful functions search for "Analytic functions" https://docs.microsoft.com/pt-br/sql/t-sql/queries/select-over-clause-transact-sql?view=sql-server-2017

1

You can do something about it

Select sum(a.VOL) from ( 
    *SUA QUERY AQUI*) a
  • went wrong, "15,3780056" when it should go "7,6890028"

  • It looks like it’s adding up twice. There’s no improper multiple in your query?

Browser other questions tagged

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