My current table looks like this
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 |
This works if im looking at one organization, but if i need to see multiple organizations I need the table to look like this. I don't have enough reputation to chat
Organization | Trainee | SvcTech | CrewChief | SvcCoord | Appr |
---|---|---|---|---|---|
Org001 | 2 | 2 | 1 | 0 | 0 |
Org002 | 2 | 1 | 0 | 0 | 0 |
This is my code
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]*/
CodePudding user response:
You may try using a pivot to achieve this eg
with cpnt_org_data as (
select Org_Id, CPNT_ID from mytable
)
select * from cpnt_org_data
pivot (
count(1)
for CPNT_ID in ('Trainee' as Trainee,'SvcTech' as SvcTech,'CrewChief' as CrewChief,'SvcCoord' as SvcCoord,'Appr' as Appr)
)
with your query, this may look like
with my_data as (
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
),
cpnt_org_data as (
select Org_Id, CPNT_ID from my_data
)
select * from cpnt_org_data
pivot (
count(1)
for CPNT_ID in ('Trainee' as Trainee,'SvcTech' as SvcTech,'CrewChief' as CrewChief,'SvcCoord' as SvcCoord,'Appr' as Appr)
)
View working demo db fiddle online
CodePudding user response:
You can use case when to pivot the table,and group by in outer layer. SQL code is following:
select ORG_ID as Organization
,sum(case when cpnt_id = 'Trainee' then 1 else 0 end) as Trainee
,sum(case when cpnt_id = 'SvcTech' then 1 else 0 end) as SvcTech
,sum(case when cpnt_id = 'CrewChief' then 1 else 0 end) as CrewChief
,sum(case when cpnt_id = 'SvcCoord' then 1 else 0 end) as SvcCoord
,sum(case when cpnt_id = 'Appr' then 1 else 0 end) as Appr
from (
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
) as a
group by ORG_ID