I am trying to find the first minimum value in the table and the date on which it occurs for each DISTINCT user in the table.
This is a table diagram 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
Since then I have been told that this is wrong, maybe someone can explain what the problem is with my SQL?
Note. I would prefer ANSI SQL if possible, but I use PostgreSQL, so if I need to use a special SQL flavor, it should work on PG.
source
share