2013-07-08 51 views
0

想知道是否有人可以幫助提供一些指導,說明如何最有效地規範化下表,以便創建可刷新的視圖/表。規範Oracle SQL中的表格

表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 

雖然這看起來很有效,但在可重複使用多個臨時表方面卻相當笨拙。

有誰知道如果一個好辦法我可以做到這一點?

+0

查找PIVOT或模型或SUM(CASE ...)。你必須在這個數百個例子。 – haki

+1

這不是正常化。事實上,它比原始表格更規範化。 – APC

+1

你的建議是通過添加一組重複的操作來對錶格進行DENORMALIZE。也許你應該考慮一下。你提出的改變會使表格更難處理,會導致代碼處理起來更加複雜和容易出錯,會在你的頭部和你的子孫後代詛咒衆神七代的憤怒,並可能導致結束我們知道的世界。祝你好運。 –

回答

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