交叉表查询与类别总计,树表结构

时间:2011-05-25 18:28:44

标签: ms-access crosstab

我如何制作这样的交叉表查询?

        TopItem1 TopItem2 Category1 Ca1Item1 Ca1Item2 Category2 Ca2Sub1 Ca2Sub1It1 Ca2Sub1It2 Ca2Sub2 Ca2Sub2It1
Group1      1        3        6         2        4        4         3       1          2          1       1
Group2      3        1        0         0        0        5         4       2          2          1       1

即。交叉表查询,每个类别的总计。

带有列标题的树结构表如下所示:

tSource

Key  Value       Parent
1    TopItem1
2    TopItem2
3    Category1
4    Category2
5    Ca1Item1    3
6    Ca1Item2    3
7    Ca2Sub1     4
8    Ca2Sub2     4
9    Ca2Sub1It1  7
10   Ca2Sub1It2  7
11   Ca2Sub2It1  8

具有行标题和要计算的值的表是您期望的那些。

现在我知道创建一个适用于任何深度树的查询是完全不可能的,但是如果我只有3个级别如图所示,那么如何获得交叉表查询以显示类别总数?

1 个答案:

答案 0 :(得分:0)

对于相对浅的“树深度”,您可以使用UNION的自我JOIN来匹配子记录中的金额与其父母和祖父母的金额。例如,使用名为[tbl]的表

Key  Value       Parent  Amount  GroupName
---  ----------  ------  ------  ---------
  1  TopItem1                 5  Group1   
  2  TopItem2                 6  Group1   
  3  Category1                7  Group1   
  4  Category2                8  Group1   
  5  Ca1Item1         3      20  Group1   
  6  Ca1Item2         3      40  Group1   
  7  Ca2Sub1          4      60  Group1   
  8  Ca2Sub2          4      80  Group1   
  9  Ca2Sub1It1       7     400  Group1   
 10  Ca2Sub1It2       7     500  Group1   
 11  Ca2Sub2It1       8     600  Group1   

查询

    SELECT t.Value, t.Amount, t.GroupName FROM tbl t
UNION ALL
    SELECT t2.Value, t1.Amount, t1.GroupName
    FROM 
        tbl t1 
        INNER JOIN 
        tbl t2
            ON t1.Parent = t2.Key
UNION ALL
    SELECT t3.Value, t1.Amount, t1.GroupName
    FROM
        (
            tbl t1 
            INNER JOIN 
            tbl t2
                ON t1.Parent = t2.Key
        )
        INNER JOIN 
        tbl t3
            ON t2.Parent = t3.Key

将返回

  • 每个“价值”本身的金额,
  • 每个“值”的子记录金额和
  • 每个“价值”的孙子记录的金额
制造

Value       Amount  GroupName
----------  ------  ---------
TopItem1         5  Group1   
TopItem2         6  Group1   
Category1        7  Group1   
Category2        8  Group1   
Ca1Item1        20  Group1   
Ca1Item2        40  Group1   
Ca2Sub1         60  Group1   
Ca2Sub2         80  Group1   
Ca2Sub1It1     400  Group1   
Ca2Sub1It2     500  Group1   
Ca2Sub2It1     600  Group1   
Category1       20  Group1   
Category1       40  Group1   
Category2       60  Group1   
Category2       80  Group1   
Ca2Sub1        400  Group1   
Ca2Sub1        500  Group1   
Ca2Sub2        600  Group1   
Category2      400  Group1   
Category2      500  Group1   
Category2      600  Group1   

所以如果我们将它包装在交叉表查询中

TRANSFORM Sum([Amount]) AS whatever
SELECT [GroupName]
FROM
    (
            SELECT t.Value, t.Amount, t.GroupName FROM tbl t
        UNION ALL
            SELECT t2.Value, t1.Amount, t1.GroupName
            FROM 
                tbl t1 
                INNER JOIN 
                tbl t2
                    ON t1.Parent = t2.Key
        UNION ALL
            SELECT t3.Value, t1.Amount, t1.GroupName
            FROM
                (
                    tbl t1 
                    INNER JOIN 
                    tbl t2
                        ON t1.Parent = t2.Key
                )
                INNER JOIN 
                tbl t3
                    ON t2.Parent = t3.Key
    )
GROUP BY [GroupName]
PIVOT [Value]

我们得到了

GroupName  Ca1Item1  Ca1Item2  Ca2Sub1  Ca2Sub1It1  Ca2Sub1It2  Ca2Sub2  Ca2Sub2It1  Category1  Category2  TopItem1  TopItem2
---------  --------  --------  -------  ----------  ----------  -------  ----------  ---------  ---------  --------  --------
Group1           20        40      960         400         500      680         600         67       1648         5         6