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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T21:16:32+00:00 2026-06-18T21:16:32+00:00

I have a fairly complex Oracle query, getting data from multiple tables. In one

  • 0

I have a fairly complex Oracle query, getting data from multiple tables. In one of the joins, I want the best record, if there is one. Therefore, a left outer join. There is a start date field, so for most records, getting the max start date will get me the best record. However, occasionally there are records that have the same start date. In that case, there is also a status field. However, the best status value is not a min or a max. ’20’ is best, ’05’ or ’40’ are ok, and ’70’ is worst. How can I set up the query to find the best option when multiple records are returned?

So, if I have the following data

Table1                       Table2
ID     otherData             ID      date      status    otherData
1      stuffa                1       jan-1-13  20        stuff93
2      stuff3                
3      stuff398              3       jan-2-13  20        stuff92
                             3       jan-2-13  70        stuff38
                             3       dec-3-12  20        stuff843

I will be able to query and get the following:

1   stuffa    jan-1-13  20  stuff93
2   stuff3
3   stuff398  jan-2-13  20  stuff92

Right now, my query is as follows, which gets a second record 3 with the 70 status:

select *
from table1 t1
left outer join
   (select *
    from table2 t2a
    where t2a.date = (select max(t2b.date)
                        from table2 t2b
                       where t2b.id = t2a.id)
    ) t2
  on (t2.id = t1.id)

Is there a way to set an ordered enumeration or something like that within a select statement? Something like

rank() over ( partition by status order by ('20','05','40','70') rank
  • 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-18T21:16:33+00:00Added an answer on June 18, 2026 at 9:16 pm

    add the status to the order by like this;

    select *
      from (select t1.id,  t1.otherdata otherdatat1, t2.date, t2.status, t2.otherdata otherdatat2,
                   rank() over (partition by t1.id order by t2.date desc,
                                case t2.status
                                  when '20' then 1
                                  when '05' then 2
                                  when '40' then 3
                                  when '70' then 4
                                  else 5
                                end) rnk
              from table1 t1
                   left outer join table2 t2
                                on t1.id = t2.id)
     where rnk = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complex LINQ query that joins several tables, and selects a
I have a fairly complex data model and consequently a fairly complex query (using
I have a fairly complex query in which I am working with with multiple
I have a fairly complex Linq query: var q = from eiods in LinqUtils.GetTable<EIOfficialDesignee>()
I have a fairly complex SQL query that pulls different types of products from
I have a fairly complex query with tons of joins all over the place.
I have a fairly complex model for one of my pages; it's made of
I have a fairly complex html form enhanced via jquery. It has multiple tabs,
I have a fairly complex view that has multiple forms, lots of validations on
I have a web page that I use to update a fairly complex data

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.