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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:55:21+00:00 2026-06-07T13:55:21+00:00

I have following data Table1 id col1 col2 col3 ———————————- 1 abc 01/01/2012 –

  • 0

I have following data

Table1

id   col1    col2       col3
----------------------------------
1    abc   01/01/2012    -
1    abc   01/01/2012    A
2    abc   01/01/2012    -
2    abc   01/02/2012    -
3    abc   01/02/2012    -
3    xyz   01/01/2012    -
4    abc   01/02/2012    -
4    xyz   01/01/2012    -
4    xyz   01/02/2012    -

following is order to evaluate –

if(col1 is false) then evaluate col2 if(col2 is false) then col3:

Col1 - xyz has first preference from all values in this column
col2 - min date
col3 - not '-' or min(col3)

I want to return only one row for each id, if col1 fails go to col2, if this fails then go to col3 condition.
From above table result should be

 id   col1    col2       col3
----------------------------------
 1    abc     01/01/2012  A
 2    abc     01/01/2012  -
 3    xyz     01/01/2012  -
 4    xyz     01/01/2012  -

I tried using dense rank but it didn’t help. I’m not sure how to perform this logic using any available function or sql logic.

for col1 - if more than one row for same code or xyz code then fail
for col2 - if more than one row with same min date then fail 
           [use this only if col1 condition fails]
  • 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-07T13:55:23+00:00Added an answer on June 7, 2026 at 1:55 pm

    You can specify many conditions to order by in your analytic function

    SELECT *
      FROM (SELECT id,
                   col1,
                   col2,
                   col3,
                   dense_rank() over (partition by id
                                          order by (case when col1 = 'xyz' 
                                                         then 1 
                                                         else 0 
                                                     end) desc,
                                                   col2 asc,
                                                   col3 asc) rnk
              FROM your_table)
     WHERE rnk = 1
    

    I’m assuming that you want dense_rank given that you used the dense_rank tag. You don’t talk about how you want to handle ties or whether ties are even possible, so it’s not clear from the question itself whether you want to use the rank, dense_rank, or row_number analytic functions. If you are only ever fetching the highest ranking row per id, rank and dense_rank will behave identically and will return multiple rows if there are ties for first place. row_number will always return a single row by arbitrarily breaking the tie. If you want to fetch rows other than the first row per id, then you’ll need to think about ties and you’ll get different behavior from rank and dense_rank. If two rows are tied for first, dense_rank will assign the third row a rnk of 2 while rank will assign it a rnk of 3.

    This seems to work for the sample data you posted

    SQL> ed
    Wrote file afiedt.buf
    
      1  with x as (
      2  select 1 id, 'abc' col1, to_date('01/01/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
      3  select 1 id, 'abc' col1, to_date('01/01/2012', 'MM/DD/YYYY') col2, 'A' col3 from dual union all
      4  select 2 id, 'abc' col1, to_date('01/01/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
      5  select 2 id, 'abc' col1, to_date('01/02/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
      6  select 3 id, 'abc' col1, to_date('01/02/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
      7  select 3 id, 'xyz' col1, to_date('01/01/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
      8  select 4 id, 'abc' col1, to_date('01/02/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
      9  select 4 id, 'xyz' col1, to_date('01/01/2012', 'MM/DD/YYYY') col2, null col3 from dual union all
     10  select 4 id, 'xyz' col1, to_date('01/02/2012', 'MM/DD/YYYY') col2, null col3 from dual
     11  )
     12  SELECT *
     13    FROM (SELECT id,
     14                 col1,
     15                 col2,
     16                 col3,
     17                 dense_rank() over (partition by id
     18                                        order by (case when col1 = 'xyz'
     19                                                       then 1
     20                                                       else 0
     21                                                   end) desc,
     22                                                 col2 asc,
     23                                                 col3 asc) rnk
     24            FROM x)
     25*  WHERE rnk = 1
    SQL> /
    
            ID COL COL2      C        RNK
    ---------- --- --------- - ----------
             1 abc 01-JAN-12 A          1
             2 abc 01-JAN-12            1
             3 xyz 01-JAN-12            1
             4 xyz 01-JAN-12            1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with data as following: id col1 col2 1 c1 1
I have the following DataTable dt = new DataTable(); dt.Columns.Add(col1, typeof(string)); dt.Columns.Add(col2, typeof(string)); dt.Rows.Add(1,
I have the following data in a table TABLE1 DOCUMENT ------ FIELD1 12345 23456
Suppose I have two tables Table1 and Table2 with the following data. Column1 Column2
I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have following data in excel table r1 r2 r3 r4 r5 v1 v2
I have the following data from 2 tables Notes (left) and scans (right) :
I have the following data on a table tbl Admission : Date and Time
Suppose we have the following table data: ID parent stage submitted 1 1 1
Lets say i have a table with the following data Customer table: Name amount

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.