开发者

Crosstab query with category totals, tree table structure

How would I go about making a crosstab query like this?

        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

i.e. a crosstab query with totals for each category.

The tree structured table with column headers is like this:

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

the tables with row headers and values to be counted are what you would expect them to be.

Now I know that it is totally impossible to create a query that would work for any depth of tree, but if I have only 3 levels like shown, how do I get a crosstab query to show the category 开发者_开发技巧totals?


For a relatively shallow "tree depth" you could use a UNION of self-JOINs to match the amounts from the child records to their parents and grandparents. For example, with a table named [tbl] containing

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   

the query

    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

would return

  • the amounts for each "Value" itself,
  • the amounts for the child records of each "Value", and
  • the amounts for the grandchild records of each "Value"

producing

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   

so if we wrap that in a crosstab query

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]

we get

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜