Click to See Complete Forum and Search --> : SELECT statement help


intrivious
05-04-2006, 03:25 PM
Here is my setup:
==============
uploads
--------
upload_id (PK)
assignment_id (FK)
student_id (FK)
grade
==============

A student can have multiple uploads per assignment.

Having all that, I need a SELECT statement to retrieve the max number of upload records per student per assignment.

For example, jonny uploads 3 times, sally 2 times, and jack 1 time.
So, there would be 6 total records per some assignment_id.

What SELECT statement could return 3, since that is the max number uploads per student, per this assignment?

Thanks for the help.

intrivious
05-04-2006, 03:39 PM
I found a solution using two queries, and I think two queries may be necesary on this one.


$numUploads = pg_query($dbconn,"SELECT student_id FROM students WHERE section_id=".$section_id);
$maxColumns = 0;
while ($row = pg_fetch_array($numUploads))
{
$numUploads2 = pg_fetch_array(pg_query($dbconn,"SELECT count(upload_id) FROM uploads WHERE student_id=".$row[0]));
if ($maxColumns < $numUploads2[0])
$maxColumns = $numUploads2[0];
}


Code is a little ugly and short-handed, sorry.
Thanks for the views, or unwritten responses ;)

chazzy
05-04-2006, 04:12 PM
select count(*),student_id,assignment_id FROM uploads GROUP BY STUDENT_ID, ASSIGNMENT_ID

intrivious
05-05-2006, 03:24 AM
That works great! Definately saves a few lines ;) ty

sridhar_423
05-05-2006, 03:57 AM
wont this suffice if only the number of records uploaded per assignment are required..
select sum(count(*)) FROM uploads GROUP BY ASSIGNMENT_ID