1

这些是我想要合并的 TBL_MATERIALS 和 TBL_PRODUCTS 的源表。根据 TBL_MATERIALS 中有多少行,这些列应该是动态的。

TBL_材料

|===============================|                               
|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_产品

|===============================|                               
|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]     |             |              |          |
|=================================================================================================|
4

2 回答 2

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
;
于 2012-06-17T17:52:04.227 回答
-1

如果我正确理解您的问题,您需要使用插入和更新公式。定义外键时,附加'on update cascade'。这使得引用的值随之更新。

于 2012-06-17T15:58:47.497 回答