-1

For example I have a report that requires February have column Created_date start at 2022-01-26 and end at 2022-02-25

How would I set up a query such that I retrieve the following table as a response:

            **Month of the year**

Feb (((Description: Start from 2021/01/26 to 2022/02/25)))

Mar (((Description: Start from 2021/02/26 to 2022/03/25)))

Apr (((Description: Start from 2021/03/26 to 2022/04/25)))

May (((Description: Start from 2021/04/26 to 2022/05/25)))

I put the description to easy understand, i dont need it in the table.

BTW I'm too fussed about the format, anything that works will do.

5
  • Does this answer your question? Postgresql query between date ranges Commented Feb 25, 2022 at 7:30
  • No, because I need it automatic for everymonth to put it on my dashboard. Commented Feb 25, 2022 at 7:35
  • So you want 4 rows with three columns? (Name of the Month, start date, end date) Commented Feb 25, 2022 at 7:40
  • No, I need only 1 column for the month. Is there any query that can put 4 rows with one column? Commented Feb 25, 2022 at 7:44
  • BTW I'm too fussed about the format, anything that works will do Commented Feb 25, 2022 at 7:46

1 Answer 1

0

This generates four rows with three columns:

select to_char(dt, 'Mon') as month,
       dt::date as start_day,
       (dt::date + interval '1 month')::date as end_day
from generate_series(date '2021-01-26', 
                     date '2021-04-26', interval '1 month') as g(dt)

If you want this as a single column, then just format and concatenate the values:

select to_char(dt, 'Mon')||' (((Description: Start from '||
       to_char(dt, 'yyyy/mm/dd')||' to '||
       to_char((dt::date + interval '1 month'), 'yyyy/mm/dd')||')))'
from generate_series(date '2021-01-26', 
                     date '2021-04-26', interval '1 month') as g(dt)
Sign up to request clarification or add additional context in comments.

1 Comment

Ok, it's working. But for example: If i want to count user_id in tbl kyc. How can i do it with that query.

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.