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

Ask A Question

Stats

  • Questions 121k
  • Answers 121k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer DateTime in SQL Server 2005 (and in SQL Server 2008… May 12, 2026 at 12:22 am
  • Editorial Team
    Editorial Team added an answer Use the GetDiskFreeSpace BOOL WINAPI GetDiskFreeSpace( __in LPCTSTR lpRootPathName, __out… May 12, 2026 at 12:22 am
  • Editorial Team
    Editorial Team added an answer I think it's a bad practice to rely on ping.exe… May 12, 2026 at 12:22 am

Related Questions

Given a set of several million points with x,y coordinates, what is the algorithm
The title pretty much sums it up, and I'm sure there's a perfectly valid
I have a web-service that I will be deploying to dev, staging and production.
I have the following tables in my database that have a many-to-many relationship, which

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.