You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
WITH RECURSIVE month_ranges AS (
SELECT
DATE_TRUNC('month', MIN("start_date"))::date AS month_start,
DATE_TRUNC('month', MAX("end_date"))::date AS max_month_end
FROM cost
UNION ALL
SELECT
(month_start + INTERVAL '1 month')::date,
(max_month_end)::date
FROM month_ranges
WHERE (month_start + INTERVAL '1 month')::date <= max_month_end
)
select month_start from month_ranges;
This SQL approach works, but I'm struggling to translate it to django-cte. I've tried using Django's aggregation to get the initial month_start and max_month_end:
Thank you for your response. I apologize for not providing more specific information initially. To answer your questions:
What error(s) are you getting?
I'm not currently experiencing a specific error, as I've been exploring different approaches to achieve the results none of them were working.
what version of Django are you using
I'm using Django 4.2.
My main question is actually about using django-cte to generate a series or range of data. Most of the examples in the documentation focus on self-referencing foreign keys, but I'm looking to create something more like a sequence of numbers or dates without writing raw SQL.
For instance, I'd like to generate a series of dates that I can use in a CTE as provided in the description. Is there a way to achieve this using django-cte? If so, could you provide some guidance or point me toward relevant examples?
My goal is to avoid writing raw SQL while still leveraging django-cte for this type of data generation. Any insights or suggestions would be greatly appreciated.
I'm trying to generate a series of month ranges using django-cte based on my Cost model. Here's what I have so far:
My goal is to generate month ranges from the minimum
start_date
to the maximumend_date
in the Cost table. For example, if I have data like this:I want to generate month ranges like:
I've tried using SQL with a recursive CTE:
This SQL approach works, but I'm struggling to translate it to django-cte. I've tried using Django's aggregation to get the initial month_start and max_month_end:
However, when I try to use this with the With class from django-cte, I encounter errors.
Could someone please help me implement this month range generation using django-cte? Any guidance or examples would be greatly appreciated.
The text was updated successfully, but these errors were encountered: