将一些列数据用于值,将一些列数据用于ID

时间:2013-08-01 14:45:56

标签: sql plsql

慢慢地但我肯定已经掌握了这一点。我使用以下查询创建了一个报告,它完美地运行。我将在查询下面给出一个结果示例。

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

1 个答案:

答案 0 :(得分:0)

我认为您可以稍微简化一下您的查询,如下所示:

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(c.category_id,36362,'X','')) As CC_Badge,
       max(decode(c.category_id,43221,'X','')) As CC_Solution,
       max(decode(c.category_id,6321, 'US','')) As US,
       max(decode(c.category_id,6081, 'CA','')) As CA,
       max(decode(c.category_id,35061,'GS','')) As GS
  From tbl_program p
  INNER Join xref_category_program xcp
    On p.program_id = xcp.program_id
  INNER Join tbl_category c
    On xcp.category_id = c.category_id
  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

我认为您不需要TBL_CATEGORY的多个连接 - 应该这样做,因为您要在WHERE子句中确定所需的类别。

我仍然对获取区域ID感到茫然。看来这个查询的大部分基本数据(PROGRAM_ID等)来自TBL_PROGRAM。是否在TBL_PROGRAM上有一列标识该区域?您可以随时进行XREF_CATEGORY_PROGRAM或TBL_CATEGORY的第二次连接,但不知道其他表中的哪个列标识了该区域,我无法帮助您了解如何将区域数据加入。

分享并享受。