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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:02:49+00:00 2026-05-22T20:02:49+00:00

I have a table with the following sample data: id user_id rank date_promoted 1

  • 0

I have a table with the following sample data:

id   user_id       rank             date_promoted
1      20        firefighter         1991-10-31
2      32        lieutenant          2003-01-03
3      34        firefighter         1990-11-23
4      20        lieutenant          1993-02-03
5      20        captain             2007-05-23
6      46        firefighter         1999-12-03

id is my primary key. user_id is a foreign key back to the users in the auth_user table.

My goal is to create a query that provides a seniority listing by rank. So, for example, to create a query for the rank ‘firefighter’, I would filter by rank for firefighter and order by date_promoted. But my problem is that user_id 20 is no longer a firefighter. He has been promoted to lieutenant and then captain. How do I, in my query, exclude him from the results of a listing of firefighters?

I’ve thought of using ‘exclude’ but that creates AND’s and just filters out the entries for captain and lieutenant for user_id, leaving the entry for firefighter for user_id 20.

I’ve also thought of using Q objects, which allows me to create OR’s. But again, that still leaves a firefighter entry for user_id 20.

I cannot figure out how to identify that user_id 20 has multiple entries and therefore remove him/her from the results for a firefighter listing.

Thanks.

  • 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-22T20:02:50+00:00Added an answer on May 22, 2026 at 8:02 pm

    Use numbers for ranks (i. e. 1 for firefighter, 2 for lieutenant and so on). Then you will easily exclude anyone who is higher (or lower) then desired rank. Or you will be able to select max (min) rank for any user with aggregation.

    If you don’t want to change your DB schema you can select it with one subquery:

    ranks = ['firefighter', 'lieutenant', 'captain']
    n = 0 # 0 - for firefighter, 1 - for lieutenant etc.    
    
    higher_users = MyModel.objects.filter(rank__in=ranks[n+1:]).values_list('user_id', flat=True)
    results = MyModel.objects.filter(rank=ranks[n]).exclude(user_id__in=higher_users).order_by('date_promoted')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a MySQL table with following data (sample); UserId - DeviceId - StartDate
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
Let's say I have a table containing following data: | id | t0 |
Please use following sample data. declare @tbl table (id int, fid int, arrival datetime,
You're help would be much appreciated... If I have the following table and sample
Let's say I have the following simple table variable: declare @databases table ( DatabaseID
I have the following PHP code doing a very simple select into a table.
I have following table structure: Table: Plant PlantID: Primary Key PlantName: String Table: Party
I have already googled for this I have a Table with following structure in
I have the following table relationship in my database: Parent / \ Child1 Child2

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.