当前5组数据在一列中如何表示 5个不同的列

How to express 5 sets of data currently in one column 5 different columns

我现在的table是这样的

CPNT_ID Org_Id Stud ID Compl_Dte
Trainee Org001 101010 Nov 13, 2016
SvcTech Org001 101010 Nov 13, 2016
CrewChief Org001 101010 Nov 13, 2016
Trainee Org001 101013 Nov 13, 2016
SvcTech Org001 101013 Nov 13, 2016
Trainee Org002 101011 Nov 13, 2016
SvcTech Org002 101011 Nov 13, 2016
Trainee Org002 101012 Nov 13, 2016

如果我正在查看一个组织,这会起作用,但如果我需要查看多个组织,我需要 table 看起来像这样。我没有足够的声誉来聊天

Organization Trainee SvcTech CrewChief SvcCoord Appr
Org001 2 2 1 0 0
Org002 2 1 0 0 0

这是我的代码

select 
cpnt.cpnt_id,
s.ORG_ID,
pc.stud_id,
pc.compl_dte
from 
        pa_stud_program sp,
        pa_program p,
        pa_student s,
        pa_stud_cpnt pc,
        ps_program_type pt,
        pa_cpnt cpnt
WHERE p.PROGRAM_SYS_GUID = sp.PROGRAM_SYS_GUID
    and pc.compl_dte is not null
    and cpnt.cpnt_id in ('Trainee','SvcTech','CrewChief','SvcCoord','Appr')
    and s.jp_id in ('1801','1805','1810','1811')
    and s.EMP_STAT_ID = 'Active'
    AND cpnt.CPNT_TYP_ID     = p.CPNT_TYP_ID
    AND cpnt.CPNT_ID         = p.CPNT_ID
    AND cpnt.REV_DTE         = p.REV_DTE
    AND pc.STUD_ID           = sp.STUD_ID
    AND sp.stud_id           = s.STUD_ID
    AND pc.CPNT_ID           = sp.CPNT_ID
    AND pc.CPNT_TYP_ID       = sp.CPNT_TYP_ID
    AND pc.REV_DTE           = sp.REV_DTE
    AND pc.seq_num           = sp.seq_num
    AND pt.PROGRAM_TYPE_ID   = p.PROGRAM_TYPE   
    /** and s.PERSON_ID_EXTERNAL  in [UserSearch]*/ 

您发布的查询与示例数据不匹配;那里没有 organization

总之:这是你应该使用的原则。将其应用于实际 returns 您发布的数据的代码。

SQL> with test (org, program, unique_users) as
  2    (select 'Store 1', 'Trainee',  1 from dual union all
  3     select 'Store 1', 'SvcTech', 12 from dual union all
  4     --
  5     select 'Store 2', 'Trainee',  2 from dual union all
  6     select 'Store 2', 'Appr'   , 11 from dual union all
  7     --
  8     select 'Store 3', 'SvcTech' ,  2 from dual union all
  9     select 'Store 3', 'CrewChief', 1 from dual union all
 10     select 'Store 3', 'SvcCoord' , 5 from dual
 11    )
 12  select org,
 13    max(case when program = 'Trainee'   then unique_users end) trainee,
 14    max(case when program = 'SvcTech'   then unique_users end) svctech,
 15    max(case when program = 'CrewChief' then unique_users end) crewchief,
 16    max(case when program = 'SvcCoord'  then unique_users end) svccoord,
 17    max(case when program = 'Appr'      then unique_users end) appr
 18  from test
 19  group by org
 20  order by org;

ORG        TRAINEE    SVCTECH  CREWCHIEF   SVCCOORD       APPR
------- ---------- ---------- ---------- ---------- ----------
Store 1          1         12
Store 2          2                                          11
Store 3                     2          1          5

SQL>