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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:18:18+00:00 2026-06-07T21:18:18+00:00

I am trying to find the first lowest value in a table, and the

  • 0

I am trying to find the first lowest value in a table, and the date on which it occurs, for every DISTINCT user in a table.

This is the table schema and some sample data:

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.0);
INSERT INTO diet_watch VALUES ('2001-01-03', 1, 122.3);
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);

The SQL I came up with is here in this snippet

I have since been informed that it is incorrect, perhaps someone can explain what the problem with my SQL is ?

Note: I would prefer ANSI SQL if possible, but I am using PostgreSQL, so if I have to use a specific flavour of SQL, it has to work 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-07T21:18:19+00:00Added an answer on June 7, 2026 at 9:18 pm

    Note: Not sure if Window functions are ANSI SQL

    WINDOW Functions are a part of the SQL:2003 specification:
    http://en.wikipedia.org/wiki/Window_function_%28SQL%29#Window_function (Thx
    @a_horse_with_no_name)

    Try this:

    http://sqlfiddle.com/#!1/7aa4e/22

    SELECT *
      FROM 
        (
         SELECT a.*, 
                ROW_NUMBER() OVER(PARTITION BY user_id ORDER BY weight) AS Position
           FROM diet_watch a
    
        ) a
        WHERE a.Position = 1 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this Grammer where i am trying to find First and Follow sets.
I am trying to find the first link of my page which url fit
I'm trying to find the fastest way to find the first non-zero value for
I'm trying to find rows where the first character is not a digit. I
I am trying to find a jQuery solution to show the first 3 items
I'm trying to make a Jquery plugin ( findFirst() ) to find the first
I'm trying to find the first property of a class that is an Integer
This is probably pretty basic, but I'm trying to find out how I can
I am trying to do a find with conditions in two models. Is this
I posted Python find first network hop about trying to find the first hop

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.