简体   繁体   English

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

[英]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 . 我在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 但是您需要为具有变量值的其他字段定义规则,例如abc

Can you post more information? 你能发布更多信息吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过mysql获取组中的第一条,最大,最小,最后一条记录 - How to get first, max, min, last records in a group by with mysql 如何获得我的数据集中每个月的最大/最小功能? - How to get MAX/MIN function for each month in my data set? 如何在数据库中获取每个主题的最小和最大日期 - How to get min & max date across the database for each subject 如何在此查询中获取每天的MAX(小时)和MIN(小时)? - How to get the MAX(Hour) and The MIN(Hour) for each day in this query? 如何对表行进行分区以获取每个进程的最小值和最大值 - How to partition table rows get the min and max for each process 如何在LINQ中获取每个日期的最大和最小输入/输出时间? - How to get Max and Min IN/OUT time for each date in LINQ? 如何获取每个月的MIN和MAX日期 - How to get the MIN and MAX date from each month 获取两列的每个唯一组合及其最大值和最小值的完整数据集 - Get whole dataset for each unique combination of two columns and their max and min values Psql:获取每个合作伙伴发票的最小、最大和计数记录,以及最后一次付款 - Psql : Get Min, Max and Count records for each partner' invoice, and last payment Sql如何获得MAX和MIN Historydata以及相应的用户 - Sql how to get MAX and MIN Historydata along with corresponding user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM