Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8464795
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:51:36+00:00 2026-06-10T14:51:36+00:00

I have three columns that look something like this: Column1 Date1 Date2 Test2 2012-06-10

  • 0

I have three columns that look something like this:

Column1       Date1         Date2
Test2         2012-06-10    
Test8         2012-05-05    2012-06-10

I’ll start off by describing my desired output given this data set. It would look like this:

Year    Month    Sum
2012    05       1
2012    06       2
2012    07       1

If Column1 contains a number (which can be found by something like: WHERE Column1 LIKE ‘%2%’), this number should be added to the month value in Date1. This is the case with the first row, i.e. a 1 is added for month 06 and 07. However, for the case of row 2, if there is a date in Date2, it should only add 1 to Date1 and every month up to and including the month in Date2. This is why a 1 is added to 05 and 06.

I’m guessing this query would use the INTERVAL function but I’m not sure as to how I should add values to months in the future.

Update:

@CraigRinger – I’ll try to explain it again. I’d like to use the number in Column1 to determine how many months into the future the date in Date1 will run. For the first row, it will run from 2012-06 to 2012-07 (as there’s a 2 in Column1).

Date2 can be seen as a cancellation date. So for row two, Date1 would continue for 8 months, but as it is being cancelled (by Date2) it runs only from 05 to 06.

In other words, the query should add 1 to the beginning month (Date1) and to every month up to and including the end (or Date2 if this exists).

I’d like to add 1 to every one of these months, so that I know the sum of months for all my rows. I’m guessing this would involve adding a date interval (equal to the number in Column1) to Date1, extracting the months from the dates between these two numbers and adding a one to them. Unfortunately I have no idea how best to implement this.

Hope I’ve explained it better this time!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T14:51:37+00:00Added an answer on June 10, 2026 at 2:51 pm

    Based on the data sample above then the following should suffice:

    create table ddata(test text, d1 date, d2 date);
    insert into ddata values ('Test2','2012-06-10'::date,null),('Test8','2012-05-05','2012-6-10')
    

    To get the number of months from the string value

    SELECT substring(test,E'\\d+$') FROM ddata;
    

    To get the difference in months where the second date is populated

    SELECT ((extract(year FROM d2) - extract(year FROM d1)) *12) + extract(month FROM d2) - extract(month FROM d1)
      FROM ddata;
    

    Putting it together using a case statement to decide which month calculation to use and generate_series to produce a list of intermediate dates to count:

    SELECT extract(year FROM ccal.cdate),
           extract(month FROM ccal.cdate),
           count(*) AS test_count
    FROM
    (SELECT generate_series(mcal.d1, mcal.d1 + CAST((mcal.num_mths || ' months') AS INTERVAL), '1 month') AS cdate
      FROM
    (SELECT d1,
           CASE (d2 IS NULL)
                WHEN TRUE THEN
                    substring(test,E'\\d+$')::integer - 1
                ELSE 
                    ((extract(year FROM d2) - extract(year FROM d1)) *12) + extract(month FROM d2) - extract(month FROM d1)
                END as num_mths
     from ddata) as mcal
    
     )AS ccal
    GROUP BY 1,2
    ORDER BY 1,2;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 3 columns that look something like this: <div style=width:900px;margin:0 auto id=container> <div
We have data that might look something like: A-10001 A-10002 A-10003 B-10001 B-10002 B-10003
So I have a chart that looks something like this. Assume that the top
I have three columns in mysql table: tax_id, company_name, store_name. I need that tax_id
I have a table in my data base that has three columns: Screen, Icon,
I have a table that has three different date columns, so I set each
I have three scripts that are in a google docs spreadsheet. In this spreadsheet,
I am working on a profit and loss report that should look like this:
Suppose that I have a table with three columns: EventID (PK) TagName TagValue I
I have three columns, y, m, and d (year, month, and day) and want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.