Results 1 to 4 of 4
  1. #1
    Willniven is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    London
    Posts
    6

    How do I sort a subquery?

    I have created a query with subqueries in order to calculate the crowding in an emergency department and how many patients are awaiting admission
    I now want to order the query in such a way that I can see when the most patients are in the department or what the maximum number of patients awaiting beds is but when I try to do this in the design view via the "sort" row, it returns an error
    This is my query so far
    SELECT T1.Patient_ID, T1.ED_Number,T1.ARRIVAL_DATETIME,

    (SELECT count(*) FROM [Attendances fin] as T2 WHERET1.ARRIVAL_DATETIME>T2.ARRIVAL_DATETIME ANDT1.ARRIVAL_DATETIME<T2.DEPART_DATETIME) AS Crowding,



    (SELECT count(*) FROM[Attendances fin] as T2 WHERE T1.ARRIVAL_DATETIME>T2.DEC_ADMIT_DATETIME ANDT1.ARRIVAL_DATETIME<T2.DEPART_DATETIME) AS AwaitingAdmit

    FROM [Attendances fin] AS T1;

    Has anyone got any smart solutions as to how I can now do this?
    Regards
    Will


  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    Why would you sort subquery? Each of the subqueries returns a single aggregate value. Why would you want these same values with every Patient record?

    Did you mean you tried to add ORDER BY clause to the outer query? What is the error message?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Willniven is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Jan 2015
    Location
    London
    Posts
    6
    Dear June7
    Thank you for your reply!
    Yes, when I tried to add the order by to the outer query it returned an error, when I tried to add the order by to the query itself, it did not actually change on the datasheet view.
    From the above SQL, I can see how many patients are in the department as every new attendance comes in, and how many patients are awaiting beds, but with 35 000 rows, it does not make for easy reading, it would be good to be able to calculate the average number of patients in the department and the average number of patients awaiting beds.
    Any ideas?!
    Regards

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,633
    What is the error message?

    Did you save that query as an object or is it just SQL statement in RecordSource? Might have to save it as a query object then reference the query object in another query or SQL statement.

    An expression with DCount() is an option but domain aggregates can be slow performers.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

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

Similar Threads

  1. Replies: 1
    Last Post: 04-07-2014, 02:59 AM
  2. sort filter, doesn't sort everything
    By deso in forum Queries
    Replies: 4
    Last Post: 01-25-2013, 05:27 AM
  3. Subquery
    By tomclavil in forum Queries
    Replies: 3
    Last Post: 02-27-2012, 03:05 AM
  4. Subquery
    By combine21 in forum Queries
    Replies: 2
    Last Post: 09-17-2010, 04:33 PM
  5. How to sort by three sort orders
    By captgnvr in forum Access
    Replies: 4
    Last Post: 11-09-2009, 07:30 AM

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