T-SQL: How to use MIN

喜欢而已 提交于 2019-12-13 07:16:12

问题


I have the following simple table, called TableA

ID  SomeVal
1   10
1   20
1   30
2   40
2   50
3   60

I want to select only those rows where SomeVal is the smallest value for the same ID value. So my results should look like this:

1   10
2   40
3   60

I think I need Group By in my SQL but am not sure how. Thanks for your help.


回答1:


SELECT ID, MIN(SomeVal)
FROM [TableName]
GROUP BY ID



回答2:


Group by will perform the aggregate function (MIN) for every unique value that is grouped by, and return the result.

I think this will do what you need:

Select ID, Min(SomeVal)
From MyTable
Group By ID


来源:https://stackoverflow.com/questions/10382384/t-sql-how-to-use-min

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!