简体   繁体   English

添加具有基于另一个日期时间列的值的日期时间列

[英]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 :使用 SQL 语言(BigQuery 方言)我需要添加一列date_today_max ,以便它从date列复制所有数据,但对于具有最新date (意思是max(date) )的记录,它将用current_date替换 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使用 Python+Pandas 我会实现类似的

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.但我不知道如何用 SQL 解决这个问题。有一个replace function,但它只接受字符串作为参数。

I think you simply want a case expression with a window function:我想你只是想要一个带有 window function 的case表达式:

select date, x,
       (case when date = max(date) over ()
             then current_date else date
        end) as date_today_max
from t;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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