我的第一个查询有什么问题?
您的查询唯一“错误”的是它仅使用 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