Get the unique records from scheduled matches in SQL Server

Viewed 34

I have a table like:

Team1 Team2
CSK SRH
SRH CSK
MI RR
RR MI
DC MI

I want to get unique records like below using MSSQL server:

Team1 Team2
CSK SRH
RR MI
DC MI
1 Answers

If the order is not important you can simply arrange the two teams alphabetically and then do a SELECT DISTINCT. Like this:

SELECT DISTINCT
CASE WHEN team1 < team2 THEN team1 ELSE team2 END AS team1,
CASE WHEN team1 < team2 THEN team2 ELSE team1 END AS team2
FROM teams;

As it's Monday morning and you are a newbie, I am being kind to you. Please note for future reference, however, when posting a question here, you are expected to show more effort yourself.

When asking a question involving SQL, it helps enormously to include in your question CREATE statements for the tables involved together with INSERT statements to enter your sample data.

And then always show what you have attempted yourself, no matter how embarassing. Everyone makes mistakes at first. Some of us 30 years later are still making mistakes. The best way to learn is to get help fixing your mistakes, but if we cannot see, what you have tried, it makes it harder to know at what level to pitch the answer.

Related