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 97395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:59:48+00:00 2026-05-10T23:59:48+00:00

I have unevenly distributed data (wrt date ) for a few years (2003-2008). I

  • 0

I have unevenly distributed data (wrt date) for a few years (2003-2008). I want to query data for a given set of start and end date, grouping the data by any of the supported intervals (day, week, month, quarter, year) in PostgreSQL 8.3.

The problem is that some of the queries give results continuous over the required period, as this one:

select to_char(date_trunc('month',date), 'YYYY-MM-DD'), count(distinct post_id)  from some_table where category_id = 1 and entity_id = 77 and entity2_id = 115  and date <= '2008-12-06' and date >= '2007-12-01' group by date_trunc('month',date) order by date_trunc('month',date);    to_char   | count  ------------+-------  2007-12-01 |    64  2008-01-01 |    31  2008-02-01 |    14  2008-03-01 |    21  2008-04-01 |    28  2008-05-01 |    44  2008-06-01 |   100  2008-07-01 |    72  2008-08-01 |    91  2008-09-01 |    92  2008-10-01 |    79  2008-11-01 |    65 (12 rows) 

But some of them miss some intervals because there is no data present, as this one:

select to_char(date_trunc('month',date), 'YYYY-MM-DD'), count(distinct post_id)  from some_table where category_id=1 and entity_id = 75 and entity2_id = 115  and date <= '2008-12-06' and date >= '2007-12-01' group by date_trunc('month',date) order by date_trunc('month',date);        to_char   | count  ------------+-------   2007-12-01 |     2  2008-01-01 |     2  2008-03-01 |     1  2008-04-01 |     2  2008-06-01 |     1  2008-08-01 |     3  2008-10-01 |     2 (7 rows) 

where the required resultset is:

  to_char   | count  ------------+-------  2007-12-01 |     2  2008-01-01 |     2  2008-02-01 |     0  2008-03-01 |     1  2008-04-01 |     2  2008-05-01 |     0  2008-06-01 |     1  2008-07-01 |     0  2008-08-01 |     3  2008-09-01 |     0  2008-10-01 |     2  2008-11-01 |     0 (12 rows) 

A count of 0 for missing entries.

I have seen earlier discussions on Stack Overflow but they don’t solve my problem it seems, since my grouping period is one of (day, week, month, quarter, year) and decided on runtime by the application. So an approach like left join with a calendar table or sequence table will not help I guess.

My current solution to this is to fill in these gaps in Python (in a Turbogears App) using the calendar module.

Is there a better way to do this?

  • 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. 2026-05-10T23:59:49+00:00Added an answer on May 10, 2026 at 11:59 pm

    You can create the list of all first days of the last year (say) with

    select distinct date_trunc('month', (current_date - offs)) as date  from generate_series(0,365,28) as offs;           date ------------------------  2007-12-01 00:00:00+01  2008-01-01 00:00:00+01  2008-02-01 00:00:00+01  2008-03-01 00:00:00+01  2008-04-01 00:00:00+02  2008-05-01 00:00:00+02  2008-06-01 00:00:00+02  2008-07-01 00:00:00+02  2008-08-01 00:00:00+02  2008-09-01 00:00:00+02  2008-10-01 00:00:00+02  2008-11-01 00:00:00+01  2008-12-01 00:00:00+01 

    Then you can join with that series.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Given a set of several million points with x,y coordinates, what is the algorithm
Have you seen library for flexible working with terminal(Unix like)? I want to implement
have such zend query: $select = $this->_table ->select() ->where('title LIKE ?', '%'.$searchWord.'%') ->where('description LIKE
have a table that dynamically generates text boxes in run time. I want to
I want to have generalised email templates. Currently I have multiple email templates with
Have set up a small website for a client however contact form submissions are
have tried a few different approaches to this but with no success so far.
have abit of a problem with Jquery UI dialog. I set it to modal:true,
Have just started using Google Chrome , and noticed in parts of our site,

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.