Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6149113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:23:50+00:00 2026-05-23T19:23:50+00:00

I have a query to fetch the nearest future doctor appointment for the users

  • 0

I have a query to fetch the nearest future doctor appointment for the users in DB2.

  SELECT user_id, 
         appointment_id, 
         date 
    FROM (SELECT user_id, 
                 appointment_id, 
                 date 
            FROM doctors_appointment 
           WHERE date > current_timestamp 
             AND user_id IN (23, 24, 25)
        ORDER BY date ASC ) as tmp_appointment 
GROUP BY user_id

But It throws error to include appointment_id and date in group by statement which means it asks to include all select fields in group by.

If I include all fields in group by the result will be like this:

   user_id    appointment_id      date 
   --------  ----------------     -------
    23            450             2011-07-20
    23            451             2011-07-26
    25            452             2011-07-18

But I need the result like below:

   user_id    appointment_id      date 
   --------   --------------       -------
    23            450             2011-07-20
    25            452             2011-07-18
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T19:23:50+00:00Added an answer on May 23, 2026 at 7:23 pm

    I think this does the trick:

      SELECT D.User_ID, D.Appointment_ID, D.Date 
        FROM Doctors_Appointment AS D
        JOIN (SELECT User_ID, MIN(Date) AS Apt_Date
                FROM Doctors_Appointment 
               WHERE Date > current_timestamp 
                 AND User_ID IN (23, 24, 25)
               GROUP BY User_ID) AS T 
          ON T.User_ID = D.User_ID AND T.Apt_Date = D.Date;
    

    It would produce two rows for a given user if said user has two appointments on the same day.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this query: public function checkUser($phone) { $sql = SELECT name FROM users
I have this query $query = mysql_query (SELECT type, count(*), date FROM tracking WHERE
I have select query that fetch record based on a condition Select * from
I have this simple query select * from users where name = 'User1' I'd
I have the following Query and i need the query to fetch data from
I have the following code $result = $handle->select()->from('store_details') ->where('store_details.store_id=?', $id) ->columns('store_details.store_name'); //->query(ZEND_DB::FETCH_OBJ); However, when
I have created this query to fetch some result from database. Here is my
I have this query var hql = @from Table1 tbl1 left join fetch tbl1.Table2;
I have the following query to fetch rows from wpr_subscribers provided that the conditions
I have a query that simply fetch data from my database, this query returns

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.