Results 1 to 3 of 3
  1. #1
    dfenton21 is offline Novice
    Windows XP Access 97
    Join Date
    May 2011
    Posts
    6

    Access SQL Subquery Problem

    Hi All,



    I have an unbelievably frustrating problem that I've been looking at for the last hour but sadly can't resolve.

    The problem is with the first subquery below called BookedPlaces. The purpose of the entire code is to return a list of records where Bookings.Status = 'Request'. The BookedPlaces subquery adds another field to the query to show the total number of bookings for that session (i.e the second field: Bookings.SessionID)

    I can't figure out how to get the subquery to count only the bookings where Bookings.SessionID = the SessionID of each record returned by the entire code. At the moment, it's counting every booking for every session (the number is the same for every record). Basically, the clause should be WHERE Bookings.SessionID = "the second field".

    I hope I've been clear, but please let me know if you have any questions.

    Any help would be greatly appreciated. I'm using Access 97 (unfortuately)


    Code:
    SELECT 
    
    Bookings.BookingID,
    Bookings.SessionID,  
    Employees.StaffName, 
    iif(Sessions.SessionCategory = 'Stand Alone', Sessions.SessionType & ' ' & 'Session', Sessions.SessionType & ' ' & 'Programme') AS SessionDescription, 
    Sessions.SessionDate, 
    (SELECT 
    COUNT(BookingID) 
    FROM Bookings 
    WHERE Bookings.Status = 'Booked') AS BookedPlaces, 
    Sessions.SessionPlaces-BookedPlaces as AvailablePlaces, 
    Employees.StaffStartDate, 
    (SELECT 
    COUNT(SessionDate) 
    FROM Sessions 
    LEFT JOIN Bookings ON Sessions.SessionID = Bookings.SessionID 
    WHERE Bookings.StaffID = Employees.StaffID AND Sessions.SessionType = 'Epos Session' AND Bookings.Attendence = 'Attended') As EposAttendence 
    
    FROM 
    Sessions 
    
    INNER JOIN (Employees INNER JOIN Bookings ON Employees.StaffID = Bookings.StaffID) ON Sessions.SessionID = Bookings.SessionID 
    
    WHERE 
    Bookings.Status = 'Request' 
    
    GROUP BY
    Bookings.BookingID, 
    Employees.StaffName, 
    Sessions.SessionPlaces, 
    Sessions.SessionCategory, 
    Sessions.SessionType, 
    Sessions.SessionDate, 
    Employees.StaffStartDate,
    Employees.StaffID,
    Bookings.SessionID

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,652
    I might join to a totals query, but try

    (SELECT
    COUNT(BookingID)
    FROM Bookings AS Alias

    WHERE Alias.Status = 'Booked' AND Alias.SessionID = Bookings.SessionID) AS BookedPlaces,
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,441
    Is there any chance you can attach a copy of your database with non sensitive information and what you want your output to look like?

    It's hard to give you coding advice without knowing table structure etc.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Subquery sum?
    By anemoskkk in forum Access
    Replies: 0
    Last Post: 04-29-2011, 12:36 PM
  2. Access Subquery Max Date/Time field
    By ruci1225 in forum Access
    Replies: 1
    Last Post: 04-08-2011, 07:33 AM
  3. SQL SubQuery Does Not Work
    By saascuba in forum Access
    Replies: 3
    Last Post: 11-04-2010, 01:59 PM
  4. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 PM
  5. Problem with subquery
    By bakerdenn in forum Queries
    Replies: 1
    Last Post: 04-24-2009, 10:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums