简体   繁体   中英

Select the highest row based on MySQL SUM result, with relations

I have the following MySQL query:

SELECT
s.student_id, s.student_firstname, s.student_lastname, s.isActive, 
    c.city_name,
    sd.student_startdate, sd.student_enddate,
    SUM(scpe.scpe_estemated_days) AS total
        FROM students s 
            INNER JOIN cityselections c ON c.city_id = s.student_city_id
            INNER JOIN studentdates sd ON sd.student_id = s.student_id
            LEFT JOIN studentcourseplan scp ON scp.student_id = s.student_id
            LEFT JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
                GROUP BY scp.cpl_id

This can output:

+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+
| student_id | student_firstname | student_lastname | isActive | city_name  | student_startdate | student_enddate | total |
+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+
| 83         | John              | Doe              | 1        | Dallas     | 2012-07-23        | 2012-09-30      | 413   |
| 84         | Derp              | Derpson          | 1        | Texas      | 2012-07-01        | 2012-08-26      | 413   |
| 85         | Barack            | Obama            | 1        | Washington | 2012-08-02        | 2012-08-31      | 2     |
| 85         | Barack            | Obama            | 1        | Washington | 2012-08-02        | 2012-08-31      | 153   |
+------------+-------------------+------------------+----------+------------+-------------------+-----------------+-------+

Now I would only like to print the row with the highest value in column total for each student_id

I tried MySQL MAX() but I couldn't make it work.

How should it be done?

Try using this query...

SELECT A.student_id, A.student_firstname, A.student_lastname, A.isActive, 
    A.city_name,
    A.student_startdate, A.student_enddate,
    MAX(A.total)
 FROM (SELECT
s.student_id, s.student_firstname, s.student_lastname, s.isActive, 
    c.city_name,
    sd.student_startdate, sd.student_enddate,
    SUM(scpe.scpe_estemated_days) AS total
        FROM students s 
            INNER JOIN cityselections c ON c.city_id = s.student_city_id
            INNER JOIN studentdates sd ON sd.student_id = s.student_id
            LEFT JOIN studentcourseplan scp ON scp.student_id = s.student_id
            LEFT JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
                GROUP BY scp.cpl_id) AS A
GROUP BY A.student_id;

Hope it helps....

I would recommend using a subquery.

SELECT s.student_id, s.student_firstname, s.student_lastname, s.isActive,
  c.city_name, sd.student_startdate, sd.student_enddate,
    (SELECT SUM(scpe.scpe_estemated_days) 
       FROM studentcourseplan scp
       LEFT JOIN studentcourseplanelements scpe ON scpe.scpe_cpl_id = scp.cpl_id
       WHERE scp.student_id = s.student_id) AS scpe_estemated_days
FROM students s
INNER JOIN cityselections c ON c.city_id = s.student_city_id
INNER JOIN studentdates sd ON sd.student_id = s.student_id;

See fiddle .

I would advise against using shubhansh's answer as it selecting attributes which are not contained in the aggregate, which is poor practice.

In standard SQL, a query that includes a GROUP BY clause cannot refer to nonaggregated columns in the select list that are not named in the GROUP BY clause. For example, this query is illegal in standard SQL because the name column in the select list does not appear in the GROUP BY:

 SELECT o.custid, c.name, MAX(o.payment) FROM orders AS o, customers AS c WHERE o.custid = c.custid GROUP BY o.custid; 

MySQL extends the use of GROUP BY so that the select list can refer to nonaggregated columns not named in the GROUP BY clause. This means that the preceding query is legal in MySQL.

See the manual .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM