Skip to main content
  1. About
  2. For Teams
Asked
Modified 4 months ago
Viewed 65 times
Part of Google Cloud Collective
0

I am trying to generate a series of dates by RECURSION in BigQuery. Unfortunately, the solution DOESN'T work !! Can someone please explain where I might be going wrong ??

Note: I understand that BigQuery provides the GENERATE_DATE_ARRAY, but just curious why my method wouldn't work (educational purpose only)!!?? Basically, I am trying to generate series of dates starting '2025-05-29' and going back to '2025-04-29'.

WITH
  RECURSIVE gen_dates AS (
  SELECT
    CURRENT_DATE() AS days
  UNION ALL
  SELECT
    DATE_SUB(days, INTERVAL 1 DAY)
  FROM
    gen_dates
  WHERE
    days >= DATE_SUB(days, INTERVAL 30 DAY) AND days <= CURRENT_DATE())
SELECT *
FROM gen_dates;

Thanks.

4
  • Why are you SHOUTING!?
    MatBailie
    –  MatBailie
    2025-05-28 19:54:09 +00:00
    Commented May 28 at 19:54
  • Please read How to Ask and then update your question with a description of what "doesn't work" means in this case (include error messages, unexpected results vs desired rests, etc).
    MatBailie
    –  MatBailie
    2025-05-28 19:56:01 +00:00
    Commented May 28 at 19:56
  • It is working! But forever )))
    Solt
    –  Solt
    2025-05-28 20:11:02 +00:00
    Commented May 28 at 20:11
  • Hem, you posted the same question as Nested Loop in SQL in BigQuery. Hopefully with @p3consulting's answer here, and mine on the other question, you'll have found and fixed your loop.
    Guillaume Outters
    –  Guillaume Outters
    2025-05-28 21:57:03 +00:00
    Commented May 28 at 21:57

1 Answer 1

1

You generate an infinite recursion because you start with the current date, go in recursion where the day is greater than itself - 30 days then decrement the day and go in recursion… you always compare the newly calculated day, not the current date of the anchor, so it's always greater than itself less 30 days.

WHERE days > DATE_SUB(CURRENT_DATE(), INTERVAL 30 DAY) should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Morty Proxy This is a proxified and sanitized view of the page, visit original site.