I need to perform a query within my table where I must compare the data entered from 3 fields, once it has been compared, return a value depending on whether they were the same or not.
Example of my table:
car_id | car_name | self_file |
---|---|---|
1 | Tsuru | 410 |
two | jetta | 410 |
3 | Altima | 410 |
In this case, when performing the query, it should return a value such as "They Are Not Equal"
On the other hand, if all the fields match (for example, that all the fields in car_name have Jetta as a record), it should return a value such as "All cars are the same"
In my query I have done it like this, but when consulting, it throws me the 3 fields with an equal or different value and I only require a single field confirming the equality or inequality.
I leave below my query that I am performing for this:
Select CASE WHEN nombre_auto = 'jetta'
THEN 'Son Iguales'
Else 'No Son Iguales'
END as Resultado
FROM autos
WHERE auto_expediente = 410
In the end, what determines whether or not they are equal is that the count of the rows it returns is 1 or more.
There are many options to do this. One that seems very educational to me.
In a common expression table, you count the rows by the criteria you need, and group them by
nombre_auto
.From the output of the cte, accounts and through a case you return one result or another.
Another option can also be.
Counts by means of a case, if the different results of the column are greater than 1.