Remove Duplicates from Two SQL Columns

Asked

Viewed 1,036 times

1

So the situation is this, I have a table and in it I have three fields, so I need to compare the (to = b) and see if there’s anyone like it. What my problem, I’m new in the middle of sql and I’m not being sure if I did it right, I tried it in the following ways.

SELECT DISTINCT [a]
               ,[b]
     FROM [Compara] -- 1 °




SELECT [a]
      ,[b]
     FROM [Compara]

WHERE [a] = [b] --2°

None have returned, so I don’t know if I did it right or wrong, if anyone can help me >.<

  • Brenda, welcome to the O.R. Your question is a bit confused, could you show the structure of your table with some data and put a result you want ? Click on edit I recommend reading on tour to understand a little more how things work here on the network!

1 answer

2


Follows:

SELECT CASE WHEN A = B THEN 'Coluna A igual a B' ELSE 'Coluna A diferente da B' END AS COMPARATIVO_01
  FROM TABELA

The SELECT above will return all table data (as I have not used the WHERE), but in return, it will return if the records in column A are different from column B, compared by row.

Now if you just want to return the records in that column A is different from B, use the instruction below:

SELECT *
  FROM TABELA
 WHERE A <> B;

Or to find out if they’re the same:

SELECT *
  FROM TABELA
 WHERE A = B;

Edited

I put the three conditions to make clear to the user the possible possibilities of SQL.

Heed: Like you said you’re still learning SQL, take care not to compare columns of different data. How to compare TRUE or FALSE with VARCHAR.

  • Thank you Lucas , and in this case the two columns are of the same type, in the case of VARCHAR.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.