Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect last known assignment but only for the latest person
    primarykey
    data
    text
    <p>I have been working on this query for a table lookup for hours and I am finally calling in for help to SO.</p> <p>There are three tables: </p> <ul> <li>computer (assetTag for ID, other information about a computer)</li> <li>student (studentID for ID, other student information)</li> <li>assignment (studentID, assetTag, issueDate)</li> </ul> <p>These are obviously linked as a many to many using assignment like a junction table:<br> Student &lt;-> assignment &lt;-> computer</p> <p>I put together a query to show a list of student names, and the last computer assignment they had. The query works great except for one problem I can't wrap my head around. If studentA is assigned computer 10000 and then turns it in, and then that computer is assigned to studentB until the end of the year when they turned it in, looking up that asset shows both students as the rightful owners of the machine. I need it to only show the latest student.</p> <p>Current query:</p> <pre><code>SELECT SQL_CALC_FOUND_ROWS s.studentID, s.lName, s.fName, s.uName, s.grade, c.assetTag, c.location, c.`status` FROM student s INNER JOIN ( SELECT studentID, MAX(issueDate) MaxDate FROM assignment GROUP BY studentID ) MaxDates ON s.studentID = MaxDates.studentID AND s.active = 1 INNER JOIN assignment a ON MaxDates.studentID = a.studentID AND MaxDates.MaxDate = a.issueDate AND a.loaner = 0 INNER JOIN computer c ON a.assetTag = c.assetTag </code></pre> <p>For the most part previous owners of a machine are inactive (s.active = 0) so there aren't very many duplicates but there shouldn't be any duplicate assetTags.</p> <p>I wish I could aggregate assetTags based off the last assignment EVER.</p> <p>Any help would be greatly appreciated since I need to get this done pretty fast.</p> <p><strong>EDIT:</strong> It may help if I explain that there are MANY students to MANY computers and MANY computers to MANY students.</p> <p>Throughout the year a student may change computers repeatedly and a computer may change to different students repeatedly.</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.
    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