Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Passing Dynamic Date Variable in Big Query| Python

I’m trying to run this bigQuery [in python]:

  start_date= "2022-03-08"
  end_date = "2022-05-03"
  query = f"""
    SELECT SUM(CostInBillingCurrency) as cost,EXTRACT(MONTH from date) as MONTH, SubscriptionId, SubscriptionName
    FROM `table_name`
    WHERE
    Date BETWEEN TIMESTAMP({start_date})
    AND TIMESTAMP({end_date})
    GROUP BY
    SubscriptionId, SubscriptionName,MONTH """

this is the error I’m getting:

google.api_core.exceptions.BadRequest: 400 No matching signature for function TIMESTAMP for argument types: INT64. Supported signatures: TIMESTAMP(STRING, [STRING]); TIMESTAMP(DATE, [STRING]); TIMESTAMP(DATETIME, [STRING]); TIMESTAMP(TIMESTAMP) at [5:18]

If I hardcode the date as:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  Date BETWEEN TIMESTAMP("2022-06-01")
  AND TIMESTAMP("2022-06-30")

it works fine. so the query is right.

please help in figuring out how to pass date dynamically.

>Solution :

Seems like the the variable is just putting values in the query, but query is expecting the string, you update your query as follows

query = f"""
SELECT SUM(CostInBillingCurrency) as cost,EXTRACT(MONTH from date) as MONTH, SubscriptionId, SubscriptionName
FROM `table_name`
WHERE
Date BETWEEN TIMESTAMP("{start_date}")
AND TIMESTAMP("{end_date}")
GROUP BY
SubscriptionId, SubscriptionName,MONTH """
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading