我有一個查詢輸出資料 - 逐行 - 對于每條記錄,我從last_modified_date列中獲取一個值,該值是該列中的最新日期,但不晚于該date列的值。我將新值保存在 column 中custom_last_modified_date。我的資料如下所示:
id date last_modified_date
A 03/01/22 2022-03-02 22:44
A 03/01/22 2022-02-01 05:14
A 03/01/01 2022-02-28 07:49
B 03/02/22 2022-03-20 07:49
B 03/02/22 2022-03-01 04:46
B 03/02/01 2022-02-28 09:24
輸出是:
id date custom_last_modified_date
A 03/01/22 02/28/22
A 03/01/22 02/28/22
A 03/01/01 02/28/22
B 03/02/22 03/01/22
B 03/02/22 03/01/22
B 03/02/01 03/01/22
這是代碼:
SELECT
id,
date,
MAX(
IF(
date(
string(
TIMESTAMP(
DATETIME(
parse_datetime('%Y-%m-%d %H:%M', last_modified_date)
),
'America/New_York'
)
)
) <= date,
date(
string(
TIMESTAMP(
DATETIME(
parse_datetime('%Y-%m-%d %H:%M', last_modified_date)
),
'America/New_York'
)
)
),
null
)
) OVER (PARTITION BY date, id) as custom_last_modified_date
FROM `my_table`
在樣本上一切正常,但是因為資料不是很干凈,有時值last_modified_date看起來像這樣:"2021-0-3 05:50"然后錯誤訊息是:Failed to parse
有沒有辦法在 where 子句中過濾掉正確的日期格式,或者排除錯誤值相同的查詢?謝謝你。
uj5u.com熱心網友回復:
您可以使用SAFE前綴忽略決議器錯誤,然后您可以過濾null行:
-- Returns null if last_modified_date not match the expected pattern
SAFE.parse_datetime('%Y-%m-%d %H:%M', last_modified_date)
從檔案:
如果您使用 SAFE 開始一個功能。前綴,它將回傳 NULL 而不是錯誤。
[...]
BigQuery 支持使用 SAFE。大多數可能引發錯誤的標量函式的前綴,包括 STRING 函式、數學函式、DATE 函式、DATETIME 函式、TIMESTAMP 函式和 JSON 函式。
更多資訊:https ://cloud.google.com/bigquery/docs/reference/standard-sql/functions-reference#safe_prefix
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/485610.html
