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

  • Home
  • SEARCH
  • 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 8313437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:29:28+00:00 2026-06-08T20:29:28+00:00

I am using PostgreSQL 8.3 . I have a table like this: id regist_time

  • 0

I am using PostgreSQL 8.3. I have a table like this:

id        regist_time        result
-----------------------------------
1     2012-07-09 15:00:08      3
2     2012-07-25 22:24:22      7
4     2012-07-07 22:24:22      8

regist_time‘s data type is timestamp.

I need to find a week time interval(start to end)
and sum(result) as num.

I want to get the result as:

      week                    num    
---------------------------------
7/1/2012-7/7/2012              10
7/8/2012-7/14/2012              5
7/15/2012-7/21/2012             3
7/22/2012-7/28/2012            11

I can get the week number just in this year:

SELECT id,regis_time, EXTRACT(WEEK FROM regis_time) AS regweek
FROM tba

The key part is

EXTRACT(WEEK FROM regis_time) 

extract function can only get the week number in this year, how can I get start time to end time in one week?

  • 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-08T20:29:29+00:00Added an answer on June 8, 2026 at 8:29 pm

    You can use date_trunc('week', ...).

    For example:

    SELECT date_trunc('week', '2012-07-25 22:24:22'::timestamp);
    -> 2012-07-23 00:00:00
    

    Then, you can convert this into a date, if you’re not interested in a start time.

    To get the end date too:

    SELECT    date_trunc('week', '2012-07-25 22:24:22'::timestamp)::date
       || ' '
       || (date_trunc('week', '2012-07-25 22:24:22'::timestamp)+ '6 days'::interval)::date;
    
    -> 2012-07-23 2012-07-29
    

    (I’ve used the default formatting here, you can of course adapt this to use MM/DD/YYYY.)

    Note that, if you want to make comparisons on timestamps, instead of using (date_trunc('week', ...) + '6 days'::interval, you might want to add an entire week and use a strict comparison for the end of the week.

    This will exclude y timestamps on the last day of the week (since the cut-off time is midnight on the day).

        date_trunc('week', x)::date <= y::timestamp
    AND y::timestamp <= (date_trunc('week', x) + '6 days'::interval)::date
    

    This will include them:

        date_trunc('week', x)::date <= y::timestamp
    AND y::timestamp < (date_trunc('week', x) + '1 week'::interval)
    

    (That’s in the rare cases when you can’t use date_trunc on y directly.)


    If your week starts on a Sunday, replacing date_trunc('week', x)::date with date_trunc('week', x + '1 day'::interval)::date - '1 day'::interval should work.

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

Sidebar

Related Questions

I am using postgresql. I have a table called custom_field_answers. The data looks like
I'm using PostgreSQL 9.1 and I have this data structure: A B ------- 1
I have a table that looks basically like this: id | redirectid | data
(I am using PostgreSQL) I have a table which stores transactions to an account.
I have developed an application using postgresql and it works well. Now I need
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I'm using PostgreSQL 9.0 and I have a table with just an artificial key
I have a table named visiting that looks like this: id | visitor_id |
I have a table with 3 columns which looks like this: File User Rating
I'm using PostgreSQL 8.4.6 with CentOS 5.5 and have a table of users: #

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.