带有类别总数的交叉表查询,树表结构

问题描述

| 我将如何进行这样的交叉表查询
        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
一个交叉表查询,其中包含每个类别的总计。 具有列标题的树形结构的表是这样的: 源
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个级别,如何获取交叉表查询显示类别总数?     

解决方法

对于相对较浅的“树深度”,您可以使用自联接的UNION来匹配子记录与父母和祖父母之间的金额。例如,一个名为[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.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.GroupName FROM tbl t
        UNION ALL
            SELECT t2.Value,t1.GroupName
            FROM 
                tbl t1 
                INNER JOIN 
                tbl t2
                    ON t1.Parent = t2.Key
        UNION ALL
            SELECT t3.Value,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