如何为每个用户获取具有最小值和最大值的唯一记录

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

我有以下 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

我需要 sql server 这个结果。我需要的是重量的最小值和重量的最大值以及每个用户一条记录。

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

只做一个分组:

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

您可以使用分组来做到这一点:

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

但是您需要为具有变量值的其他字段定义规则,例如 abc

你能post了解更多信息吗?