ettapi
ettapi

Reputation: 51

Add Grand Total Row of Every Column BigQuery SQL

I have this table

    Row     Type      ValueA        ValueB       ValueC
---------+---------+-----------+-------------+-------------
    1    |    A    |   1000    |    50       |     10
    2    |    B    |   2000    |    40       |     20

My desired results are

      Row     Type      ValueA        ValueB       ValueC
    ---------+---------+-----------+-------------+-------------
        1    |    A    |   1000    |    50       |     10
        2    |    B    |   2000    |    40       |     20 
 Grand Total |         |   3000    |    90       |     30

Is there any way to do it? Tried using case function but it adds more column not row. Any help would be appreciated. Thanks

Upvotes: 1

Views: 1478

Answers (1)

Mikhail Berlyant
Mikhail Berlyant

Reputation: 173171

try below simple approach

select * from your_table union all
select 'Grand Total', null, sum(ValueA), sum(ValueB), sum(ValueC)
from your_table            

if applied to sample data as in your question

with your_table as (
  select '1' `Row`, 'A' Type, 1000 ValueA, 50 ValueB, 10 ValueC union all
  select '2', 'B', 2000, 40, 20 
)             

output is

enter image description here

Upvotes: 3

Related Questions