在Oracle SQL中规范化表

时间:2013-07-08 11:19:22

标签: sql oracle plsql normalization normalize

想知道是否有人可以协助提供一些关于如何最有效地规范化下表的指导,以便我可以创建一个可刷新的视图/表。

表1:

SYSTEM_KEY | ID | ORDER | ORDER_STATUS | SYSTEM_Actions
A             1   Pencil   Open          Shipped   
B             1   Pencil   Open          Tested  
C             1   Pencil   Open          Shipped    
A             1   Paper    Closed        Delivered

我希望以可重复的方式将此表规范化为类似这样的事情:

结果:

ID | ORDER | Order Status | A_actions | B_Actions | C_Actions 
1    Pencil  OPEN           Shipped     Tested      Delivered                   
1    Paper   Closed         Delivered    null       null

我能够通过做类似的事情来实现这个目标

Select full.ID, full.order, full.orderstatus, case when system_ID = 'A' then sysa.system_actions as A_actions, ....{for B, C} 
from table1 full
left join table1 sysa on full.id = sysa.id and full.order = sysa.order
left join table1 sysb on full.id = sysb.id and full.order = sysb.order  

虽然这似乎有效,但在重复使用多个临时表方面非常笨拙。

有谁知道我能做到这一点的好方法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用group by子句

select id,order,order_status,
       case when system_ID = 'A' then system_actions as A_actions,
       case when system_ID = 'B' then system_actions as B_actions,
       case when system_ID = 'C' then system_actions as C_actions
from table1
group by id,order,order_status