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 6768013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:01:17+00:00 2026-05-26T15:01:17+00:00

users: user_id user_name ——————— 1 User A 2 User B tracking: user_id track ———————

  • 0

users:

user_id    user_name     
---------------------

 1          User A
 2          User B

tracking:

user_id     track         
---------------------

 1           no
 2           no

applications:

user_id    date_of_application   date_ended    grade    status    
---------------------------------------------------------------

 1            2011-01-01         2011-02-28     1.0     Ended
 1            2011-02-02         2011-03-28     1.0     Ended
 1            2011-03-03         2011-04-28     (1.5)   Ended

 2            2011-01-01         2011-02-20     2.0     Ended
 2            2011-02-02         2011-03-11     2.5     Ended
 2            2011-03-03         2011-04-28     (1.0)   Ended

 1            2011-05-10              -          -      Pending
 2            2011-05-15              -          -      Pending
  • note that the table can contain multiple records of the same user as long as all its previous applications have ended (status = ended)
  • user_id is not unique (applies to the applications table only)
  • date is in yy-mm-dd format
  • date_ended and grade are only updated the instant the application has ended
  • also, I understand that it probably is recommended for ‘status’ to have its own table, however I would prefer that the above tables are taken as is (minus the typos and significant errors of course)

What I want to accomplish here is to retrieve all rows WHERE status is ‘Pending’ and such that the value for the grade column for each of these retrieved rows is the value of the latest grade (in other words the row with the latest date_ended), (in parenthesis above) where status is ‘Ended’ for this particular user (or row).

Also, I would need to have the first 10 rows of the result to be ORDERed BY grade ASC. And have the succeeding rows after that (11th row up to the final row) to be ORDERed BY date_of_application ASC.

Clearly SQL queries isn’t my strongest area so I’m not sure if it’s better (or is only possible) to perform those ORDER BY(s) using 2 or more queries. I however prefer this to be done using a single query only.

The desired result:

user_id   user_name   date_of_application   grade   status   track 
--------------------------------------------------------------------

 1         User A         2011-05-10        (1.5)    Pending    no
 2         User B         2011-05-15        (1.0)    Pending    no

Working code I have so far on my end [minus the possible typos], (and listed are additions to be applied):

  • latest grade
  • ORDER BY grade (first 10), ORDER BY date_of_application (11th up to last row)

Query:

SELECT users.user_name,
       t.track,
       a.user_id,
       a.date_of_application,
       a.status,
       (SELECT ae.grade
          FROM applications AS ae
          WHERE ae.status = 'Ended' 
            AND ae.user_id = a.user_id                                                            
         LIMIT 1) AS grade
  FROM users
  JOIN applications AS a ON users.user_id = a.user_id
  JOIN tracking AS t ON users.user_id = t.user_id
 WHERE a.status = 'Pending'
ORDER BY grade ASC
  • 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-26T15:01:17+00:00Added an answer on May 26, 2026 at 3:01 pm

    You probably trying to do too much in one query here.

    Anyway, if you want something to hurt your eyes:

     select a.* from
     (
     SELECT u.user_name,
     a.user_id,
     a.date_of_application,
     td.grade,
     a.status,
     t.track
     FROM users u
     JOIN applications AS a ON u.user_id = a.user_id
     JOIN tracking AS t ON u.user_id = t.user_id
     LEFT OUTER JOIN
     (
     select ap.user_id,ap.grade 
     from applications ap
     inner join
     (select a.user_id,max(date_ended) as max_ended_date
     from applications a
     where a.status = 'Ended'
     group by a.user_id
     ) md on md.user_id = ap.user_id and ap.date_ended = md.max_ended_date
     ) as td on u.user_id = td.user_id
     WHERE a.status = 'Pending'
     ORDER BY cast(replace(replace(td.grade,'(',''),')','') as decimal(12,2)),u.user_id ASC
     LIMIT 10
     ) a
     WHERE grade is not null
     UNION ALL
     select b.* from
     (
     SELECT u.user_name,
       u.user_id,
       a2.date_of_application,
       td.grade,
       ifnull(a2.status,'No applications yet') as status,
       t2.track
     FROM users u
     LEFT OUTER JOIN (select user_id,date_of_application,status from applications where     status = 'Pending') AS a2 ON u.user_id = a2.user_id
     JOIN tracking AS t2 ON u.user_id = t2.user_id
     LEFT OUTER JOIN
     ( 
     select ap.user_id,ap.grade 
     from applications ap
     inner join
      (select a.user_id,max(date_ended) as max_ended_date
      from applications a
      where a.status = 'Ended'
      group by a.user_id
     ) md on md.user_id = ap.user_id and ap.date_ended = md.max_ended_date
     ) as td on u.user_id = td.user_id
     where u.user_id not in (
     select t1.user_id
     from (
     select ap1.user_id,ap1.grade 
      from applications ap1
      inner join
      (select a1.user_id,max(date_ended) as max_ended_date
       from applications a1
       where a1.status = 'Ended'
       group by a1.user_id
      ) md1 on md1.user_id = ap1.user_id and ap1.date_ended = md1.max_ended_date
      order by cast(replace(replace(ap1.grade,'(',''),')','') as decimal(12,2)),md1.user_id asc
      limit 10
      ) as t1
     )
     ORDER BY status desc,a2.date_of_application ASC
     ) b;
    

    This does make the following assumptions:

    1. There is always only one row for each user_id in the users and
      tracking table

    EDIT

    To explain this query a bit:

    Inline view aliased a (aka ‘The Top Half’) brings back a list of the top 10 users according to their most recent ‘ended’ grade ascending. Note the following part of the query that strips any brackets from the grade, converts the resulting number to a decimal to 2 decimal places and orders them ascending by grade and then, in case of equal grade scores, by user_id:

    ORDER BY cast(replace(replace(td.grade,'(',''),')','') as decimal(12,2)),u.user_id ASC
    

    Inline view b is pretty much the same as inline view a except that excludes users that would appear in The Top Half and orders the results by status DESC (to move those users with no applications to the bottom of the list) and date of application ASC.

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

Sidebar

Related Questions

I have two tables: Users: ID, first_name, last_name Networks: user_id, friend_id, status I want
I have the following tables: users: user_id user_name message: message_id thread_id to_id from_id title
I have three tables to define users: USER: user_id (int), username (varchar) USER_METADATA_FIELD: user_metadata_field_id
I have 2 following tables: Users (user_id, user_name, ... ) Photo (photo_id, user_id, ...)
I have 3 tables users(id,name),groups(id,name) and users_groups(user_id,group_id). users and groups have many to many
I have 3 tables: users (id, name) currency (id, name) accounts (id, user_id, currency_id,
Say I have the following: $query = SELECT user_id,email,password, roles.role_id,role_name FROM users JOIN roles
I have a user table 'users' that has fields like: id first_name last_name ...
My problem is like this, I have 3 tables: - users (user_id, name) -
I have a users table, and a view table which lists some user ids...

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.