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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:17:46+00:00 2026-06-10T11:17:46+00:00

I need to make a query which displays an organizations order descreasing in order

  • 0

I need to make a query which displays an organizations order descreasing in order by the organizations’ rank.
However, if organizations have an event that is currently happening, they are displayed at the top (ordered by rank)
Then organizations with upcoming events are displayed
And lastly organizations with nothing happening are displayed.

An organization can have both currently happening and upcoming events, and have multiples of each, but it only chooses one event and the organization only appears under on category

Currently happening is a priority over upcoming, so the organization only appears in currently happening if it has both currently happening and upcoming events

Here is an example of what the output should be

Currently Happening

  • Organization 1 (Rank 3) – Event1
  • Organization 2 (Rank 2) – Event2
  • Organization 3 (Rank 1) – Event3

Upcoming

  • Organization 5 (Rank 3) – Event4
  • Organization 4 (Rank 2) – Event5
  • Organization 6 (Rank 2) – Event6

Nothing

  • Organization 7 (Rank 3) – Event7
  • Organization 8 (Rank 2) – Event8
  • Organization 9 (Rank 2) – Event9

Here is an example table set-up

Organizations
id – name – rank

Events
id – organization_id – name – status (2 = currently happening, 1 = upcoming, 0 = nothing)

The status column just contains either 2, 1, or 0 as I said which I believes makes it possible to do an ORDER BY

If someone could write an example query I would greatly appreciate it! I’m having trouble with ordering the events by the status and then ordering the organizations of those events by their rank at the same time.

  • 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-06-10T11:17:47+00:00Added an answer on June 10, 2026 at 11:17 am

    Example:

    SELECT o.*, max(e.status) AS org_status
    FROM   organization o
    JOIN   event e ON e.organiszation_id = o.organization_id
    GROUP  BY o.organization_id
    ORDER  BY org_status DESC, o.rank DESC, o.name;
    

    Note that I use organization_id as primary key of organization as it is an anti-pattern to use the non-descriptive name id in multiple tables across the database.

    The fact that o.organization_id is the primary key also allows me to only GROUP BY o.organization_id – this works since PostgreSQL 9.1.

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

Sidebar

Related Questions

I have a Linq query that returns an anonymous type, which I then use
I need to make an sql query which tries to update a row by
Currently I have to make multiple GETs to receive all the information which I
I need to make a query output Using table, I want the following report,
I need to make a query like this: SELECT PNPDeviceID FROM Win32_NetworkAdapter WHERE AdapterTypeId
I need to make a query to one of Google's services. I read this
I'm playing with GAE, and need to make a query with something like this:
I need to make a mySQL query and am not sure what the format
So I need to make 3 tabs with query (got this part down), but
I need a SQL query to make all data in a column UPPER CASE?

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.