Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Group by only second Column

Tags:

sql

sql-server

I want to only group by the second column so there are no duplicates:

ID | Name
__________
1  | Test1
2  | Test2
3  | Test3
4  | Test2
5  | Test1
6  | Test4

So I only want to show:

ID | Name
__________
1  | Test1
2  | Test2
3  | Test3
6  | Test4

I tried using Distinct and group by with no success

like image 332
Standage Avatar asked Feb 14 '26 04:02

Standage


1 Answers

This should work using the MIN aggregate:

SELECT MIN(ID), Name
FROM YourTable
GROUP BY Name
  • SQL Fiddle Demo
like image 61
sgeddes Avatar answered Feb 15 '26 16:02

sgeddes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!