Home > Software engineering >  Display only the columns of two different select statements containing one row?
Display only the columns of two different select statements containing one row?

Time:01-06

I have two select statements which both have one row but an fixed number of columns (15).

So far I have tried using intersect but it doesn't seem to do what I want

select 'ABC' as test, 'DEF' as test2, '123' as test3

select 'ABC2' as test, 'DEF' as test2, '124' as test3

Comparing both will display the a string will the differences

The result will the the columns with the different values as a string. In this case:

"'test': 'ABC', 'test3': '123'"

CodePudding user response:

Use a bunch of case expressions to compare each column and output a value if different e.g.

select
  case when a.test1 <> b.test1 then 'test1:'   a.test1 else '' end
    case when a.test2 <> b.test2 then ', test2:'   a.test2 else '' end
    case when a.test3 <> b.test3 then ', test3:'   a.test3 else '' end
from (
    select 'ABC' as test1, 'DEF' as test2, '123' as test3
) a
cross join (
  select 'ABC2' as test1, 'DEF' as test2, '124' as test3
) b;

Returns:

Result
test1:ABC, test3:123
  • Related