简体   繁体   中英

Bigquery Table Lead Column Values Based On Date

I want to duplicate a (revenue) column and shift it one year in order to make YoY comparisons. Currently looking to lead values in a Big Query table based on a specific date to achieve this but stuck.

I used DATE_ADD to create a new column to get the date of last year but now I want to get a column next to it with the revenue based on the DATE_ADD date.

One problem is that not all locations include the same date, that's why it's harder to make the shift.

There is no way to properly format a table so I have an image of the intended result here. Where basically the revenue_last_year should fill in with the value of the revenue column corresponding to the date_add column and the right location.

在此处输入图片说明

The query below is as far as I've been able to go:

SELECT  
Date, 
location, 
revenue,
DATE_ADD(date, INTERVAL -1 YEAR) AS DateAdd,
LEAD(revenue, ##OFFSET## ) OVER (PARTITION BY location ORDER BY date DESC) AS revenue_last_year
FROM
`dataset.table1`

Does anyone have a suggestion on how to relate the offset value to the right date? Or should I approach this in a completely different way?

Below is for BigQuery Standard SQL

#standardSQL
SELECT 
  a.date, a.location, a.revenue, 
  DATE_SUB(a.date, INTERVAL 1 YEAR) date_last_year, 
  IFNULL(b.revenue, 0) revenue_last_year 
FROM `project.dataset.table` a
LEFT JOIN `project.dataset.table` b
ON a.location = b.location
AND DATE_SUB(a.date, INTERVAL 1 YEAR) = b.date

You can test, play with above using dummy data as in below example

#standardSQL
WITH `project.dataset.table` AS (
  SELECT DATE '2018-02-20' `date`, 'A' location, 1 revenue UNION ALL
  SELECT '2018-02-20', 'B', 2 UNION ALL
  SELECT '2018-02-21', 'A', 3 UNION ALL
  SELECT '2018-02-22', 'B', 4 UNION ALL
  SELECT '2019-02-20', 'A', 5 UNION ALL
  SELECT '2019-02-20', 'B', 6 UNION ALL
  SELECT '2019-02-21', 'A', 7 UNION ALL
  SELECT '2019-02-21', 'B', 8 UNION ALL
  SELECT '2019-02-22', 'A', 9 UNION ALL
  SELECT '2019-02-22', 'B', 10 
)
SELECT 
  a.date, a.location, a.revenue, 
  DATE_SUB(a.date, INTERVAL 1 YEAR) date_last_year, 
  IFNULL(b.revenue, 0) revenue_last_year 
FROM `project.dataset.table` a
LEFT JOIN `project.dataset.table` b
ON a.location = b.location
AND DATE_SUB(a.date, INTERVAL 1 YEAR) = b.date
-- ORDER BY a.date, a.location  

with result

Row date        location    revenue date_last_year  revenue_last_year    
1   2018-02-20  A           1       2017-02-20      0
2   2018-02-20  B           2       2017-02-20      0
3   2018-02-21  A           3       2017-02-21      0
4   2018-02-22  B           4       2017-02-22      0
5   2019-02-20  A           5       2018-02-20      1    
6   2019-02-20  B           6       2018-02-20      2    
7   2019-02-21  A           7       2018-02-21      3    
8   2019-02-21  B           8       2018-02-21      0
9   2019-02-22  A           9       2018-02-22      0
10  2019-02-22  B           10      2018-02-22      4    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM