我慢慢地但肯定地掌握了这个窍门。我使用以下查询创建了一份报告,效果很好。我将在查询下方给出一个结果示例。
Select p.parent_program_id, p.program_id, p.name, p.copyright, p.active_flag,
max(Case when c.category_id = 36261 Then 'X' Else ' ' End) As CC_Indicator,
max(Case when c1.category_id = 36362 Then 'X' Else ' ' End) As CC_Badge,
max(Case when c2.category_id = 43221 Then 'X' Else ' ' End) As CC_Solution
From tbl_program p
Join xref_category_program xcp
On p.program_id=xcp.program_id
left Join tbl_category c
On xcp.category_id=c.category_id and c.category_id = 36261
left Join tbl_category c1
On xcp.category_id=c1.category_id and c1.category_id = 36362
left Join tbl_category c2
On xcp.category_id=c2.category_id and c2.category_id = 43221
where xcp.category_id in(36261,36362,43221)
group by p.program_id, p.name, p.copyright, p.active_flag, p.parent_program_id
Order by p.program_id
结果如下所示(为简洁起见,省略了一些不重要的列):
P.ParentProgramID P.ProgramID CC_Indicator CC_Badge CC_Solution
00001 12111 X
null 20200 X X X
null 00001 X X
指标/徽章/解决方案由类别 ID 标识。现在,请求此报告的人希望它按公司排序,这些公司也在同一列中分配了类别 ID。
假设公司的 xcp.category_id 值为 11111/22222/33333/44444,我将如何按照报告接收者决定的顺序将其放入此报告的另一列?
看起来像这样的东西,假设总共有 6 条记录......
P.ParentProgramID P.ProgramID CC_Indicator CC_Badge CC_Solution Region
00001 12111 X 44444
null 20200 X X X 44444
null 00001 X X 22222
null 32332 X 11111
null 44215 X X 11111
null 84425 X 33333
第二次努力,如下所述...
Select p.parent_program_id, p.program_id, p.name, p.copyright, p.active_flag,
max(decode(c.category_id,36261,'X','')) As CC_Indicator,
max(decode(c1.category_id,36362,'X','')) As CC_Badge,
max(decode(c2.category_id,43221,'X','')) As CC_Solution,
max(decode(c3.category_id,6321,'US','')) As US,
max(decode(c4.category_id,6081,'CA','')) As CA,
max(decode(c5.category_id,35061,'GS','')) As GS
From tbl_program p Join xref_category_program xcp On p.program_id=xcp.program_id
left Join tbl_category c On xcp.category_id=c.category_id and c.category_id = 36261
left Join tbl_category c1 On xcp.category_id=c1.category_id and c1.category_id = 36362
left Join tbl_category c2 On xcp.category_id=c2.category_id and c2.category_id = 43221
left Join tbl_category c3 On xcp.category_id=c3.category_id and c3.category_id = 6321
left Join tbl_category c4 On xcp.category_id=c4.category_id and c4.category_id = 6081
left Join tbl_category c5 On xcp.category_id=c5.category_id and c5.category_id = 35061
where xcp.category_id in(36261,36362,43221,6321,6081,35061)
group by p.program_id, p.name, p.copyright, p.active_flag, p.parent_program_id
Order by p.program_id