Note that there are some explanatory texts on larger screens.

plurals
  1. POSelecting the minimum of two distinct values
    primarykey
    data
    text
    <p>I'm working on a sql selection where I need to get all the smallest records for an ID. My problem is that, while I thought my code was working, it would often return incorrect values for other columns, evidently because of my failure to use having to support my aggregate min function. Here is my code.</p> <pre><code>SELECT * FROM example_students INNER JOIN (SELECT id, student, test, subject, MIN(score), semester FROM example_student_scores WHERE studentid=94 GROUP BY studentid, test, subject) as scores ON example_students.id=scores.studentid </code></pre> <p>This is what I want my code to do.</p> <blockquote> <p>select from exampletable the minimum score for each distinct test and subject combination where the student has id#94</p> </blockquote> <p>Here are two (heavily modified) tables for the example (I changed all the column names here and in my code too.</p> <pre><code>example_students id name ----+-----------+ 94 Bob 1023 Thomas </code></pre> <p>.</p> <pre><code>example_students_scores id studentId test subject score semester ----+-----------+-----------+-----------+-----------+----------- 0 94 quiz math 46 fall 1 94 quiz math 71 fall 2 94 quiz math 63 winter 3 94 midterm math 94 winter 4 94 midterm science 72 fall 5 94 quiz math 50 spring 6 94 final math 76 spring 7 1023 quiz math 6 spring 8 1023 quiz math 52 winter 9 1023 quiz science 68 fall ..* </code></pre> <p>and the results should be</p> <pre><code>results id studentId test subject score semester ----+-----------+-----------+-----------+-----------+----------- 0 94 quiz math 46 fall 3 94 midterm math 94 winter 4 94 midterm science 72 fall 6 94 final math 76 spring </code></pre> <p>The problem is, I'll get the wrong values for the semester column (and all the other columns I have in my live work).</p> <p>Given that this has taken me a long time to get no-where with, here is sql to create the two example databases:</p> <pre><code>drop table if exists example_students; drop table if exists example_students_scores; create table example_students( id int(10) primary key, name char(25) ); create table example_students_scores( id int(10) not null, studentId int(10) not null, test char(20), subject char(20), score int(10) not null default '0', semester char(20), primary key (id), index studentid (studentid) ); insert into example_students values ('94','Bob'); insert into example_students values ('1023','Thomas'); insert into example_students_scores values ('0' ,'94' ,'quiz' ,'math' ,'46' ,'fall'); insert into example_students_scores values ('1' ,'94' ,'quiz' ,'math' ,'71' ,'fall'); insert into example_students_scores values ('2' ,'94' ,'quiz' ,'math' ,'63' ,'winter'); insert into example_students_scores values ('3' ,'94' ,'midterm' ,'math' ,'94' ,'winter'); insert into example_students_scores values ('4' ,'94' ,'midterm' ,'science' ,'72' ,'fall'); insert into example_students_scores values ('5' ,'94' ,'quiz' ,'math' ,'50' ,'spring'); insert into example_students_scores values ('6' ,'94' ,'final' ,'math' ,'76' ,'spring'); insert into example_students_scores values ('7' ,'1023' ,'quiz' ,'math' ,'6' ,'spring'); </code></pre> <p>I'd be grateful for any pointers or tips, it's very embarrassing to only figure out that your work is wrong a week after going live!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload