Domanda

I tried:

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);

but when i provided the values in it...it worked perfectly

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);

What's wrong in my 1st query ?

È stato utile?

Soluzione

What's wrong in my 1st query ?

The only thing "wrong" with your query is that it simply uses syntax that Access SQL does not support. The IN clause following the PIVOT keyword only supports a static list of comma-separated column headings (ref: here).

Edit re: comments

As a workaround, you could convert the "guts" of your current Crosstab query into a regular Select query...

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

...and save that query as [qry_BookingsBase]. Let's imagine that said query returns...

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

We can create another saved query in Access named [qry_BookingsPadded] to "pad out" the data and ensure that there is at least one row for every room...

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

...returning...

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

Now you can just create a Crosstab query named [qry_BookingsCrosstab] against that "padded" query...

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

...which returns...

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                              

...and if that first row of all Null values is a problem then you could filter that out with

SELECT * FROM qry_BookingsCrosstab WHERE [Date] IS NOT NULL
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top