0

I want to generate date data using postgresql function "generate_series" , however I have only advanced as far as the following:

SELECT
    ( DATE_TRUNC( 'month', ld ) + '1 month'::INTERVAL - '1day'::INTERVAL )::DATE AS pe_produc
FROM
    GENERATE_SERIES( TIMESTAMP'2022-01-31', TIMESTAMP'2022-3-31', INTERVAL'1 month' ) AS ld

The result of the previous query:

enter image description here

While the date structure I want to generate is as follows:

enter image description here

How should I modify my query to get the desired result?

Thanks in advance.

2
  • It should be generated by the function generate_series. Commented Oct 17, 2022 at 0:38
  • The same date mes_prod should be repeated for each different date in pe_prod through the generate_series function. Commented Oct 17, 2022 at 0:41

1 Answer 1

-1

With cte command I was able to fix it!

SELECT 
         a.pe_produc::date AS pe_produc
        ,b.mes_produc::date AS mes_produc
FROM 
    (SELECT
     generate_series('2022-01-31'::date
                     , '2022-03-31'::date
                     , interval  '1 month') AS pe_produc
     ,generate_series(1, (DATE_PART('month', '2022-03-31'::date) - DATE_PART('month', '2022-01-31'::date)+1)::int) AS b
     ) A 
    ,(SELECT generate_series('2022-01-31'::date
                       ,'2022-03-31'::date
                       , interval  '1 month') AS mes_produc 
     , generate_series(1, (DATE_PART('month', '2022-03-31'::date) - DATE_PART('month', '2022-01-31'::date)+1)::int) AS b
      ) B
Sign up to request clarification or add additional context in comments.

1 Comment

That is not a CTE statement. That is joined sub queries.

Your Answer

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.