ASP YOK
ASP YOK

Reputation: 131

Calculate difference between months in SQL

I'm using Big Query and Data Studio to build a query for a pivoted sum over months, like:

> Name|Jan|Feb|Mar|...
> abc |123|234|676|...
> SUM |123|234|676|..

But what I need now is a difference between two months :

> Name|Jan|Feb |Mar |...
> abc |123| 234| 676|...
> SUM |123| 234| 676|..
> Diff|0  |+111|+442|..

How can achieve this the best and most efficient way?

Upvotes: 0

Views: 110

Answers (1)

Gordon Linoff
Gordon Linoff

Reputation: 1269963

You seem to want a basic airthmetic:

with t as (
      <your query here>
     )
select name, jan, feb, mar, . . .
from t
union all
select concat(name, '-DIFF'), 0, feb - jan, mar - feb, . . . 
from t;

Upvotes: 1

Related Questions