Johann
Johann

Reputation: 29867

T-SQL: How to use MIN

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.

Upvotes: 0

Views: 1184

Answers (2)

Joon
Joon

Reputation: 2147

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

Upvotes: 0

Curtis
Curtis

Reputation: 103358

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

Upvotes: 1

Related Questions