MS Access PIVOT with User Defined Field

时间:2013-06-29 22:18:22

标签: sql ms-access pivot

你们任何一个好心人都请帮忙!!

我需要查询下面显示的源表。 (NULL是空白字段)

UNUM Ticket Overflow 
1    135    NULL
1    136    NULL
1    137    NULL
1    138    NULL
1    NULL   2b
2    135    NULL
2    136    NULL
2    137    NULL
3    135    NULL
3    136    NULL
3    137    NULL
3    138    NULL
3    139    NULL
3    140    NULL
3    NULL   66a
4    NULL   12a
5    NULL   14a

我需要生成输出,如下所示。

UserNum Ticket1 Ticket2 Ticket3 Ticket4 Ticket5 Ticket6 Ticket7 Ticket8 Ticket9 Overflow
1       135     136     137     138     Null     Null   Null    Null    Null    2b
2       135     136     137     Null    Null     Null   Null    Null    Null    Null      
3       135     136     137     138     139      140    Null    Null    Null    66a   
4       Null    Null    Null    Null    Null     Null   Null    Null    Null    12a       
5       Null    Null    Null    Null    Null     Null   Null    Null    Null    14a
  1. 源表具有分配给用户的多个票证。总共最多有9张门票。
  2. 用户要么有票证,要么有溢出,但每个用户只能溢出。
  3. 我有问题将Ticket列中的数据转移到预定义的字段名称,如Ticket1,Ticket2 ......

1 个答案:

答案 0 :(得分:0)

(对于这个答案,我使用[TicketJournal]作为源表的名称。)

首先,我们需要为交叉表(pivot)准备数据。我们可以通过聚合源表上的不等自连接来生成“Tickets”行,然后在源表上使用简单的SELECT进行UNION-ing来实现“Overflows”

    SELECT 
        UNUM AS UserNum, 
        CStr(Ticket) AS Item, 
        "Ticket" & COUNT(Ticket) AS Destination
    FROM
        (
            SELECT tj1.UNUM, tj1.Ticket 
            FROM 
                TicketJournal tj1 
                INNER JOIN 
                TicketJournal tj2 
                    ON tj1.UNUM=tj2.UNUM 
                        AND tj1.Ticket>=tj2.Ticket
        )
    GROUP BY UNUM, Ticket
UNION ALL
    SELECT
        UNUM AS UserNum,
        Overflow AS Item,
        "Overflow" AS Destination
    FROM TicketJournal
    WHERE Overflow IS NOT NULL

将该查询保存在Access中[TicketCrosstabSource],然后运行它以验证它是否返回

UserNum  Item  Destination
-------  ----  -----------
      1  135   Ticket1    
      1  136   Ticket2    
      1  137   Ticket3    
      1  138   Ticket4    
      2  135   Ticket1    
      2  136   Ticket2    
      2  137   Ticket3    
      3  135   Ticket1    
      3  136   Ticket2    
      3  137   Ticket3    
      3  138   Ticket4    
      3  139   Ticket5    
      3  140   Ticket6    
      1  2b    Overflow   
      3  66a   Overflow   
      4  12a   Overflow   
      5  14a   Overflow 

现在我们可以针对[TicketCrosstabSource]查询进行交叉表查询...

TRANSFORM First(TicketCrosstabSource.[Item]) AS FirstOfItem
SELECT TicketCrosstabSource.[UserNum]
FROM TicketCrosstabSource
GROUP BY TicketCrosstabSource.[UserNum]
PIVOT TicketCrosstabSource.[Destination] In ("Ticket1","Ticket2","Ticket3","Ticket4","Ticket5","Ticket6","Ticket7","Ticket8","Ticket9","Overflow");

...返回

UserNum  Ticket1  Ticket2  Ticket3  Ticket4  Ticket5  Ticket6  Ticket7  Ticket8  Ticket9  Overflow
-------  -------  -------  -------  -------  -------  -------  -------  -------  -------  --------
      1  135      136      137      138                                                   2b      
      2  135      136      137                                                                    
      3  135      136      137      138      139      140                                 66a     
      4                                                                                   12a     
      5                                                                                   14a