user8787011
user8787011

Reputation:

How to choose a row for specific column

I have a table that contains multiple rows with duplicate ID values:

ID Course1 Course2
S1 A1 A2
S1 A1 B1
S2 D1 B1
S2 C1 B1

I need to select the first row for each unique ID value, resulting in a table that includes only the first occurrence of each ID.

The desired resulting table should look like this:

ID Course1 Course2
S1 A1 A2
S2 D1 B1

Upvotes: 0

Views: 37

Answers (1)

WiLLyxVKei
WiLLyxVKei

Reputation: 113

The easiest way (in my opinion) to select the first row for each ID is to use a GROUP BY statement. This will group the rows by their ID and allow you to select the first row for each group. Here is an example query:

SELECT *
FROM YourTable
GROUP BY ID;
Result:

[Picture of the result

Upvotes: 1

Related Questions