sqlsqltransaction

Group by and filter on 2 Distinct values in SQL


I have table T1

ID Size
A  1
A  2
A  3
B  3
B  4
C  2
C  4

I want to group by ID and filter the smallest size for each ID

Desired outcome:

A 1
B 3
C 2

I tried doing something like this:

SELECT ID, Size
FROM T1
WHERE ID IN (SELECT DISTINCT ID FROM T1)

Solution

  • You want a basic GROUP BY query:

    SELECT ID, MIN(Size) AS Size
    FROM T1
    GROUP BY ID;