Skip to content
Advertisement

Add datetime column with values based on another datetime column

I have a table:

|       date | x |
|------------+---|
| 2020-09-09 | 1 |
| 2020-09-09 | 2 |
| 2020-10-10 | 3 |
| 2020-10-10 | 4 |
| 2020-10-10 | 5 |
| 2020-11-11 | 6 |
| 2020-11-11 | 7 |

Using SQL language (BigQuery dialect) I need to add one column date_today_max, such that it copies all data from date column, but for records with the latest date (meaning max(date)) it will replace date with current_date:

|       date | date_today_max | x |
|------------+----------------+---|
| 2020-09-09 |     2020-09-09 | 1 |
| 2020-09-09 |     2020-09-09 | 2 |
| 2020-10-10 |     2020-10-10 | 3 |
| 2020-10-10 |     2020-10-10 | 4 |
| 2020-10-10 |     2020-10-10 | 5 |
| 2020-11-11 |     2020-11-15 | 6 |
| 2020-11-11 |     2020-11-15 | 7 |

with Python+Pandas I’d achieve similar with

In [23]: from datetime import datetime

In [24]: import pandas as pd

In [25]: d = pd.date_range("2020-10-10","2020-10-15",freq="1d")

In [26]: df = pd.DataFrame(zip(d,[1,2,3,4,5,6]), columns=['date','x'])

In [27]: df['date_today_max'] = df['date'].replace(df['date'].max(),datetime.now().replace(hour=0,minute=0,second=0,microsecond=0))

In [28]: df
Out[28]:
        date  x date_today_max
0 2020-10-10  1     2020-10-10
1 2020-10-11  2     2020-10-11
2 2020-10-12  3     2020-10-12
3 2020-10-13  4     2020-10-13
4 2020-10-14  5     2020-10-14
5 2020-10-15  6     2020-11-15

but I have no clue how to tackle this with SQL. There is a replace function, but it only accepts strings as parameters.

Advertisement

Answer

I think you simply want a case expression with a window function:

select date, x,
       (case when date = max(date) over ()
             then current_date else date
        end) as date_today_max
from t;
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement