Results 1 to 3 of 3
  1. #1
    ET123 is offline Novice
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    1

    Calculating Max appointments

    I'm currently trying to this query in MS Access but can't. Pls help

    1) Query to show the providers that have the highest # patients that come back for in person visit within 7 days of telephone visit. I've work on it below but can't run because of error. Can anyone help me to fix this?

    SELECT a.provider_id, max(a.provider_id) as Max_provider_id, a.patient_id, a.appointment_type, a.appointment_date, a.show_code, a.appointment_time
    FROM [Appointment Data] a
    WHERE a.appointment_type='in-person visit'
    and a.APPOINTMENT_DATE >= #12/1/2009# and a.appointment_date < #6/1/2010# and a.show_code = 'Y'
    and exists(select appointment_date from [Appointment Data] where patient_id=a.patient_id and (appointment_type='telephone visit' and show_code = 'y' and appointment_date >= #12/1/2009# and appointment_date <#6/1/2010# and appointment_time >= #17:30#) and datediff('d',appointment_date,a.appointment_date) <= 7)
    group by provider_id;

    Error Message is: you tried to execute a query that does not include the specified expression 'patient_id' as part of an aggregated function



    Thank you!

  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,521
    It looks like you need GROUP BY clause. Generally speaking, when you have aggregate functions like Sum, Max, etc in the SELECT clause, you need a GROUP BY clause. Any field which which is not part of an aggregate function (like patient_id) would be in the GROUP BY clause. If you hit the Totals button with the query in design view, you should see where you can set that.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    NTC is offline VIP
    Windows Vista Access 2007
    Join Date
    Nov 2009
    Posts
    2,392
    pbaldy advice is always correct. but let me add - since you self describe as a novice; your 'where' statement is complex. For the novice - these type queries don't have to be accomplished in one sql statement. Consider using the query design grid and first make a plain ole select query for "person visit within 7 days of telephone visit"...make that work and give it a name. then do an aggregate query using this first query as the record source (look for the epsilon E icon). Any Access textbook will have an example of aggregate (sum, count, max) queries. This approach is generally more accessible to beginners.

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

Similar Threads

  1. Calculating Networkdays
    By jsiketa in forum Queries
    Replies: 1
    Last Post: 06-10-2010, 06:05 PM
  2. Calculating Dates
    By TC0126 in forum Queries
    Replies: 1
    Last Post: 02-23-2010, 08:13 PM
  3. Calculating the column value
    By access in forum Queries
    Replies: 6
    Last Post: 08-20-2009, 11:51 AM
  4. Calculating the sum of every four records.
    By Alanlives in forum Queries
    Replies: 0
    Last Post: 07-31-2009, 05:56 AM
  5. Calculating Values
    By Jahan in forum Queries
    Replies: 1
    Last Post: 07-09-2006, 09:15 AM

Tags for this Thread

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