Oracle:分析:如何使用某些列名进行排序

Oracle: Analytics: How to Sort Using Some Column Names

我有一个与下面相同的查询。

select organization_name, is_manager, is_vendor, is_engineer
from table_org_roles;

示例数据:

organization_name|is_manager|is_vendor|is_engineer
---------------------------------------------------
Org_A            |Yes       |No       |No         
Org_B            |No        |Yes      |No         
Org_C            |No        |No       |Yes
Org_D            |Yes       |No       |Yes

不过我想创建一个 SQL 查询,该查询将按升序的角色名称(即列名称)排序。如何进行如下显示的查询:

Roles(ASC) | Organization
-------------------------
is_engineer| Org_C
is_engineer| Org_D
is_manager | Org_A
is_manager | Org_D
is_vendor  | Org_B

是否可以使用 Analytics 进行此类查询? 非常感谢您。

试试这个未经测试的查询:

select  * (
select organization_name, is_manager case is_manager WHEN Yes THEN "is_manager" ELSE "" END As Roles from  table_org_roles where is_manager = "YES"
union all
select organization_name, is_engineer case is_engineer WHEN Yes THEN "is_engineer " ELSE "" END from  table_org_roles where is_engineer= "YES"
union all
select organization_name, is_vendor case is_vendor WHEN Yes THEN "is_vendor" ELSE "" END from  table_org_roles where is_vendor= "YES"
) order by Roles

您好,这也可以借助 UNPIVOT 操作来完成。见下文。

   create table test_stack
(
organization_name varchar2(100),
is_manager varchar2(10),
is_vendor varchar2(20),
is_engineer varchar2(20)
);
/
insert into test_stack
select 'org_A','Yes','No','No' from dual
union all
select 'org_B','No','Yes','No' from dual
union all
select 'org_C','No','No','Yes' from dual
union all
select 'org_D','Yes','No','Yes' from dual; 




select organization_name,roles
    from test_stack
    unpivot include nulls ( existence for Roles in ( IS_MANAGER as 'IS_MANAGER',IS_VENDOR as 'IS_VENDOR',IS_ENGINEER as 'IS_ENGINEER'))
    where existence ='Yes';