如何在带条件的 ORACLE Select 语句中将单条记录行转换为列

How to convert single record row to column using in ORACLE Select statement with condition

我需要在前面显示列名。基于列中 select 语句中满足的条件的列具有我需要的列作为一个记录的值。为此,我正在尝试编写数据库查询。你能帮忙生成这个查询吗

有附加条件的逆轴;第 1 - 4 行代表示例数据(您已经有了,所以您的查询从第 5 行开始):

SQL> with test (id, emp_name, is_maths_passed, is_science_passed, is_social_passed) as
  2    (select 123, 'Robert', 'YES', 'NO' , 'YES' from dual union all
  3     select 345, 'Devid' , 'NO' , 'YES', 'YES' from dual
  4    )
  5  select code, passed
  6  from (select * from (select * from test where id = &par_id)
  7        unpivot (passed
  8                 for code
  9                 in (is_maths_passed   as 'is_maths_passed',
 10                     is_science_passed as 'is_science_passed',
 11                     is_social_passed  as 'is_social_passed'
 12                    )
 13                )
 14       )
 15  where passed = 'YES';
Enter value for par_id: 123

CODE              PAS
----------------- ---
is_maths_passed   YES
is_social_passed  YES

SQL> /
Enter value for par_id: 345

CODE              PAS
----------------- ---
is_science_passed YES
is_social_passed  YES

SQL>