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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:13:56+00:00 2026-06-07T23:13:56+00:00

I have an SQL table that looks like this: CREATE TABLE diet_watch ( entry_date

  • 0

I have an SQL table that looks like this:

CREATE TABLE diet_watch (
  entry_date date NOT NULL,
  user_id    int default 1,
  weight     double precision NOT NULL
);

INSERT INTO diet_watch VALUES ('2001-01-01', 1, 128.2);
INSERT INTO diet_watch VALUES ('2001-01-02', 1, 121.2);
INSERT INTO diet_watch VALUES ('2001-01-03', 1, 100.6);
INSERT INTO diet_watch VALUES ('2001-01-04', 1, 303.7);
INSERT INTO diet_watch VALUES ('2001-01-05', 1, 121.0);
INSERT INTO diet_watch VALUES ('2001-01-01', 2, 121.0);
INSERT INTO diet_watch VALUES ('2001-01-06', 2, 128.0);
INSERT INTO diet_watch VALUES ('2001-01-07', 2, 138.0);
INSERT INTO diet_watch VALUES ('2001-01-01', 3, 128.2);
INSERT INTO diet_watch VALUES ('2001-01-02', 3, 125.5);
INSERT INTO diet_watch VALUES ('2001-01-03', 3, 112.8);
INSERT INTO diet_watch VALUES ('2001-01-06', 3, 111.2);

I further have this table:

CREATE TABLE summing_period (
    user_id INT NOT NULL, 
    start_date DATE NOT NULL, 
    end_date DATE NOT NULL); 

insert into summing_period VALUES (1, '2001-01-01', '2001-01-03'); 
insert into summing_period VALUES (2, '2001-01-02', '2001-01-06'); 
insert into summing_period VALUES (3, '2001-01-03', '2001-01-06'); 

I want to write a query that returns DISTINCT ROWS with the following columns:

  • the user_id
  • the sum of the weights in table diet_watch between the specified dates in table summing_period (for the user_id)

So the result of the query based on the data in table summing period should be:

1,350.0
2,128.0
3,224.0

Unfortunately, this time, I have reached the limit of my SQLfu – and I no idea how to even get started in writing the SQL. Ideally, the solution should be ANSI SQL (i.e. db agnostic). however, since I am developing to a PostgreSQL 8.4 backend, if the solution is db centric, it must at least run on PG.

  • 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-07T23:13:58+00:00Added an answer on June 7, 2026 at 11:13 pm
    SELECT
        sp.user_id, SUM(dw.weight)
    FROM
        summing_period sp,
        diet_watch dw
    WHERE
        dw.user_id = sp.user_id AND
        dw.entry_date >= sp.start_date AND
        dw.entry_date <= sp.end_date
    GROUP BY
        sp.user_id
    

    What this query does is join each diet_watch row to the row in summing_period that matches its user_id and whose date falls in the summing_period‘s range.

    The SELECT then asks for the SUM of the weights for each different user_id (as a result of the GROUP BY user_id).

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

Sidebar

Related Questions

Hi there I have an SQL table that looks like this: CREATE TABLE IF
I have a SQL table set that looks like this create table foo (
I have this table that looks like this CREATE TABLE `purchases` ( `id` INT(10)
I have a table that looks something like this: CREATE TABLE student_results(id integer, name
I have a table with data that looks like this: create table demo_patient_info (
I'm using SQL Server 2008. I have a database table that looks like this
We have a table that looks roughly like this: CREATE TABLE Lockers { UserID
I have some nicely-structured data that looks like this: CREATE TABLE SourceBodyPartColors ( person_ID
SQL 2008 I have a commission table that looks like this: Comm% | ProfitStartRange
I have a legacy SQL schema which looks something like this: CREATE TABLE `User`

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.