如何从另一个表的行生成“动态”列的查询表?

时间:2012-06-17 14:33:40

标签: sql database-design datatable report

这些是来自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]     |             |              |          |
|=================================================================================================|

2 个答案:

答案 0 :(得分: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
;

答案 1 :(得分:-1)

如果我正确理解您的问题,您需要在插入和更新时使用公式。定义外键时,请附加“更新级联”。这使得引用的值随之更新。