I'm using a Vertex AI notebook, running BigQuery IPython magics and injecting parameters.
params = {"day_of_week": "WEDNESDAY"}
%%bigquery sales_df --project $project_id --params $params
SELECT created_date, num_sales
FROM sales
WHERE DATE_DIFF(created_date, "2023-01-01", WEEK(@day_of_week)) BETWEEN -12 and 11
I'd expect this to work but it doesn't because WEEK
expects a WEEKDAY
argument ( BQ docs ). Hardcoding it as WEEK(WEDNESDAY)
works but the parameter injection doesn't because it adds quotes around the string, effectively compiling it as WEEK("WEDNESDAY")
.
Is there any workaround here? I can't find a BQ method that turns a string weekday ( "WEDNESDAY"
) into the day of week enum ( WEDNESDAY
).
You might try a dynamic SQL in your BigQuery magic.
params = {"day_of_week": "WEDNESDAY"}
%%bigquery sales_df --project $project_id --params $params
EXECUTE IMMEDIATE FORMAT("""
SELECT created_date, num_sales
FROM sales
WHERE DATE_DIFF(created_date, "2023-01-01", WEEK(%s)) BETWEEN -12 and 11
""", @day_of_week);
below is a working example I've tried.
%%bigquery --project your-project-id --params {"day_of_week": "WEDNESDAY"}
EXECUTE IMMEDIATE FORMAT("""
SELECT DATE_DIFF(CURRENT_DATE, "2023-01-01", WEEK(%s)) BETWEEN -12 and 11
""", @day_of_week)
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.