HiveBrain v1.2.0
Get Started
← Back to all entries
principlesqlModerate

Compare 4 or more columns values

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
valuescomparemorecolumns

Problem

I have a table like:

ID   A0  A1  A2 A3 A4
14   A   B   A  C  A
15   A   A   A  A  A


I need to compare all 5 columns and check if they have same value and retrive the only one that have all same values....in this case the one with ID 15.
Is there a way to accomplish that in T-SQL?

Solution

I'm not familiar with SQL Server's brand of
SQL, but it must be something like:

SELECT ID FROM My_Table 
WHERE 
(A0 = A1) AND
(A1 = A2) AND
(A2 = A3) AND
(A3 = A4);


Check out this dbfiddle.uk which shows all the answers in the answer thread.

Code Snippets

SELECT ID FROM My_Table 
WHERE 
(A0 = A1) AND
(A1 = A2) AND
(A2 = A3) AND
(A3 = A4);

Context

StackExchange Database Administrators Q#104521, answer score: 12

Revisions (0)

No revisions yet.