Hatik
Hatik

Reputation: 1159

Selecting only one minimum value for each repeating value

I have a table like below:

CODE | DATA |
-----+------+--
asd  | 1    |
asd  | 2    |
asd2 | 3    |
asd4 | 3    |
asd4 | 2    |

I am trying to select the minimum data for each code, so that only one should be selected for the given code, wanted result is

CODE | DATA |
-----+------+--
asd  | 1    |
asd2 | 3    |
asd4 | 2    |

I have tried

Select code, min(data) from myTable group by code, data;

The result I get is only grouping of similar codes but not selecting the minimum of data only

Upvotes: 1

Views: 35

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521249

Just aggregate over the code, but not the data:

SELECT code, MIN(data) AS min_data
FROM myTable
GROUP BY code;

Upvotes: 2

Related Questions