简体   繁体   中英

how to get the unique records with min and max for each user

I have the following table:

id  gender  age highest weight  lowest weight   abc
a   f       30  90              70              1.3
a   f       30  90              65              null
a   f       30  null            null            1.3
b   m       40  100             86              2.5
b   m       40  null            80              2.5
c   f       50  105             95              6.4

I need this result in sql server . What I need is the minimum of the weight and maximum of the weight and one record per user.

id  gender  age highest weight  lowest weight   abc
a   f       30  90              65              1.3
b   m       40  100             80              2.5
c   f       50  105             95              6.4

Just do a grouping:

select id, 
       max(gender), 
       max(age), 
       max([highest weight]), 
       min([lowest weight]), 
       max(abc)
from SomeTable
group by id

You can do this using grouping:

select id, gender, max(highest_weight), min(lowwest_weight) from student
group by id, gender

But you need do define the rule for the other fields with variable value, like abc

Can you post more information?

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