交叉表查询 - 基于相关表动态派生列标题

时间:2013-10-14 00:46:33

标签: sql ms-access ms-access-2007 crosstab

我试过了:

TRANSFORM First([FirstName] & " " & [LastName] & " - " & [Status]) AS Name
SELECT qry_DateTemp.Date
FROM Customers RIGHT JOIN qry_DateTemp ON Customers.CustomerID = qry_DateTemp.CustomerID
GROUP BY qry_DateTemp.Date
PIVOT qry_DateTemp.RoomNumber In (select RoomNo from Room);

但是当我提供其中的值时...它完美地运作

TRANSFORM First([FirstName] & " " & [LastName] & " - " & [Status]) AS Name
SELECT qry_DateTemp.Date
FROM Customers RIGHT JOIN qry_DateTemp ON Customers.CustomerID = qry_DateTemp.CustomerID
GROUP BY qry_DateTemp.Date
PIVOT qry_DateTemp.RoomNumber In (101,102,103,104,105,106,201,202);

我的第一次查询出了什么问题?

1 个答案:

答案 0 :(得分:2)

  

我的第一次查询出了什么问题?

您的查询唯一“错误”是它只使用Access SQL不支持的语法。 PIVOT关键字后面的IN子句仅支持以逗号分隔的列标题的静态列表(参考:here)。

编辑:评论

作为一种解决方法,您可以将当前交叉表查询的“内容”转换为常规的选择查询...

SELECT
    First([FirstName] & " " & [LastName] & " - " & [Status]) AS Name,
    qry_DateTemp.Date,
    First(qry_DateTemp.RoomNumber) AS RoomNo
FROM
    Customers 
    RIGHT JOIN 
    qry_DateTemp 
        ON Customers.CustomerID = qry_DateTemp.CustomerID
GROUP BY qry_DateTemp.Date

...并将该查询保存为[qry_BookingsBase]。让我们假设所述查询返回...

Name                       Date        RoomNo
-------------------------  ----------  ------
Gord Thompson - confirmed  2013-10-15     101
Bob Loblaw - tentative     2013-10-16     102
Bob Loblaw - tentative     2013-10-17     102

我们可以在Access中创建另一个名为[qry_BookingsPadded]的已保存查询,以“填充”数据,并确保每个房间至少有一行......

SELECT 
    qry_BookingsBase.[Name], 
    qry_BookingsBase.[Date],
    Room.[RoomNo] AS RoomNumber
FROM
    Room
    LEFT JOIN
    qry_BookingsBase
        ON Room.RoomNo = qry_BookingsBase.RoomNo

...返回...

Name                       Date        RoomNumber
-------------------------  ----------  ----------
Gord Thompson - confirmed  2013-10-15         101
Bob Loblaw - tentative     2013-10-17         102
Bob Loblaw - tentative     2013-10-16         102
                                              103
                                              104
                                              105
                                              106
                                              201
                                              202

现在您可以针对“填充”查询创建一个名为[qry_BookingsCrosstab]的交叉表查询...

TRANSFORM First([Name]) AS FirstOfName
SELECT qry_BookingsPadded.Date
FROM qry_BookingsPadded
GROUP BY qry_BookingsPadded.Date
PIVOT qry_BookingsPadded.RoomNumber

...返回......

Date        101                        102                     103  104  105  106  201  202
----------  -------------------------  ----------------------  ---  ---  ---  ---  ---  ---

2013-10-15  Gord Thompson - confirmed                                                      
2013-10-16                             Bob Loblaw - tentative                              
2013-10-17                             Bob Loblaw - tentative                              

...如果所有Null值的第一行都有问题,那么您可以使用

过滤掉它
SELECT * FROM qry_BookingsCrosstab WHERE [Date] IS NOT NULL