0

todo:I grouped a set of data by c_id and took the data for the maximum value of the height of each group. problem:The data that does not display the maximum value correctly like c_id=3 in picture no.2. enter image description here enter image description here

I want to ask how to fix it, thanks.

D.Todd
  • 63
  • 1
  • 7
  • If performance is important, consider a solution along these lines: http://sqlfiddle.com/#!9/096589/1 Note the index on c_id, height - and for next time see: [Why should I provide an MCVE for what seems to me to be a very simple SQL query?](https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql-query) – Strawberry Jul 30 '18 at 11:03

3 Answers3

1

You can check this query out:

SELECT `id`, `name`, `gender`, `age`, `c_id`, MAX(`height`) AS height 
FROM `student` 
GROUP BY `c_id` 
ORDER BY `height` DESC;

Here is the Demo

Sinto
  • 3,915
  • 11
  • 36
  • 70
  • Thank you very much for your help. – D.Todd Jul 30 '18 at 10:54
  • The values returned for id, name, gender, and age in this query are indeterminate. They may be correct, but only by luck. The manual covers this in considerable detail. – Strawberry Jul 30 '18 at 11:10
1
select * 
  from student
 where (c_id, age,height) in ( Select c_id
                                    , max(age) age
                                    , max(height) as height 
                                 from student 
                                group 
                                   by c_id )
Strawberry
  • 33,750
  • 13
  • 40
  • 57
Zaynul Abadin Tuhin
  • 31,407
  • 5
  • 33
  • 63
0

Try this

Select id, name, gender, age, c_id, height
From student
Where (c_id, age) in (Select c_id, max(age) from student group by c_id)
Order by height desc
Gaj
  • 888
  • 5
  • 5