2012-06-17 38 views
1

這些是來自TBL_MATERIALS & TBL_PRODUCTS的源表,我想合併。 根據TBL_MATERIALS中的行數,這些列應該有些動態。如何使生成的「動態」列從另一個表的行查詢表?

TBL_MATERIALS

|===============================|        
|MATERIAL   |  Gram |       
|-------------------------------|        
|Flour-Hard   |  25 |    
|Flour Soft   |  76 |     
|Sugar-White  |  25 |      
|Sugar-Washed  |  15 |     
|Sugar-Brown  |  10 |      
|CalciumPropionate |  2.5 |         
|SodiumBenzoate  |  2 |        
|TartarCream  |  5 |      
|MilkSkimmed  |  20 |     
|===============================|          

TBL_PRODUCTS

|===============================|        
|Product   |  Batch |        
|-------------------------------|             
|Ameriloaf   |  5  |       
|Peter Pann   |  2.5 |        
|Chizmada   |  3  |       
|Ubemada   |  8  |        
|Millionaire  |  9  |      
|Sweet Maria  |  2.5 |       
|Butter Tarts  |  1.25 |        
|Caramel Croquette |  4  |         
|Garlic Stick  |  11 |        
|===============================| 

這就是我想要的表格看起來應該像。 QUERY_CUSTOM的列應該是動態的,當我在TBL_MATERIALS上添加新項目時,應該顯示下表中的新列。我想我們會使用像CREATE TABLE之類的東西。我仍在研究這件事。 我希望這會幫助你理解。

QUERY_CUSTOM                 
|=================================================================================================| 
|Product   |  Batch | Flour-Hard | Flour-Soft | Sugar-White | Sugar-Washed | etc.etc. |  
|-------------------------------------------------------------------------------------------------|  
|Ameriloaf   |  5  | Gram*Batch | ALL BLANK |    |    |   | 
|Peter Pann   |  2.5 |ex.25*2.5=75| CELLS  |    |    |   | 
|Chizmada   |  3  |  "  | SHOULD  |    |    |   | 
|Ubemada   |  8  |  "  | BE FILLED |    |    |   | 
|Millionaire  |  9  |  "  | WITH  |    |    |   | 
|Sweet Maria  |  2.5 |  "  |[GRAM]  |    |    |   | 
|Butter Tarts  |  1.25 |  "  | MULTIPLIED |    |    |   | 
|Caramel Croquette |  4  |  "  | BY CORRESP.|    |    |   | 
|Garlic Stick  |  11 |  "  |[BATCH]  |    |    |   | 
|=================================================================================================| 
+0

是否所有的材料都與所有產品一起使用?我沒有看到指示哪些產品使用哪種產品的關係 – Sparky

+0

是的所有產品。我剛剛填補了第一個。但全部都會被填滿。 例如.. 行向>百萬富翁具有柱 - >糖白色所以該公式將變成: [批量] * [革蘭]所以... 9 * 25 = 225 – jestrange

+0

我已經編輯/澄清現在的樣品。 – jestrange

回答

-1

如果我正確理解您的問題,則需要在插入和更新時使用公式。定義外鍵時,附加'更新級聯'。這使得參考值隨之更新。

+0

不幸的是,我對這些並不是很熟悉。你能發佈代碼嗎?或者舉個例子。不管怎麼說,還是要謝謝你。 – jestrange

0
select 
     Product 
    , Batch 
    , sum(case when Material = 'Flour-Hard'  then Product_Grams else 0.0 end) as Flour_Hard 
    , sum(case when Material = 'Flour-Soft'  then Product_Grams else 0.0 end) as Flour_Soft 
    , sum(case when Material = 'Sugar-White'  then Product_Grams else 0.0 end) as Sugar_White 
    , sum(case when Material = 'Sugar-Washed'  then Product_Grams else 0.0 end) as Sugar_Washed 
    , sum(case when Material = 'Sugar-Brown'  then Product_Grams else 0.0 end) as Sugar_Brown 
    , sum(case when Material = 'CalciumPropionate' then Product_Grams else 0.0 end) as CalciumPropionate 
    , sum(case when Material = 'SodiumBenzoate' then Product_Grams else 0.0 end) as SodiumBenzoate 
    , sum(case when Material = 'TartarCream'  then Product_Grams else 0.0 end) as TartarCream 
    , sum(case when Material = 'MilkSkimmed'  then Product_Grams else 0.0 end) as MilkSkimmed 
from (
    select 
      a.Product 
     , a.Batch 
     , b.Material 
     , b.Gram 
     , (a.Batch * b.Gram) as Product_Grams 
    from  TBL_PRODUCTS as a 
    cross join TBL_MATERIALS as b 
) as xx 
group by Product, Batch 
order by Product 
; 
+0

感謝您的嘗試,但這不是我正在尋找的答案 您是否可以讓它變成動態的,當我在TBL_Materials中添加新項目時,將在QUERY_CUSTOM表中顯示一個新列。不管怎麼說,多謝拉。 – jestrange

+0

@JesKeshlyCruz;我看到,這可以用動態SQL「簡單地」生成,這裏是一個示例 http://stackoverflow.com/questions/1914303/sql-syntax-to-pivot-multiple-tables/1915438#1915438 –

相關問題