【问题标题】:How to express 5 sets of data currently in one column 5 different columns [duplicate]如何在一列中表示5组数据5个不同的列[重复]
【发布时间】:2021-12-13 02:14:19
【问题描述】:

我当前的表是这样的

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

如果我查看一个组织,这很有效,但如果我需要查看多个组织,我需要表格看起来像这样。我没有足够的声望来聊天

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]*/ 

【问题讨论】:

    标签: sql oracle pivot oracle10g


    【解决方案1】:

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

    无论如何:这是您应该使用的原则。将其应用于实际返回您发布的数据的代码。

    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>
    

    【讨论】:

    • 让我修复我原来的帖子
    • 不要。只需应用我向您展示的内容即可。
    猜你喜欢
    • 2021-12-13
    • 2019-04-06
    • 2014-05-20
    • 2023-01-12
    • 1970-01-01
    • 2019-01-06
    • 1970-01-01
    • 2018-10-29
    • 1970-01-01
    相关资源
    最近更新 更多