Skip to content
Advertisement

Add datetime column with values based on another datetime column

I have a table:

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:

with Python+Pandas I’d achieve similar with

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:

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement