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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T07:00:56+00:00 2026-06-10T07:00:56+00:00

I have the following data in a simple table: ID | TYPE ——— 1

  • 0

I have the following data in a simple table:

ID | TYPE
---------
1  | 1
1  | 2
2  | 1
3  | 2
4  | 1
4  | 2
4  | 3
5  | 1
5  | 3
6  | 3

I now need to query this data in the following way:

  • For each ID, only one row should be returned
    • If there is only one row for the ID return it.
    • If there are multiple rows for the ID, return the one with TYPE = 2. If that doesn’t exist, return the one with TYPE = 3.
  • TYPE can be either 1, 2 or 3
  • ID and TYPE have a composite unique index, meaning that per ID there can be a maximum of three rows.

Question: How would I put this logic in a query?


Expected result:

ID | TYPE
---------
1  | 2
2  | 1
3  | 2
4  | 2
5  | 3
6  | 3
  • 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-10T07:00:58+00:00Added an answer on June 10, 2026 at 7:00 am

    Oracle has analytic functions in all recent versions.

    WITH
      sequenced AS
    (
      SELECT
        *,
        ROW_NUMBER() OVER (PARTITION BY id
                               ORDER BY CASE type WHEN 2 THEN 1
                                                  WHEN 3 THEN 2
                                                  WHEN 1 THEN 3 END) AS sequenced_id
      FROM
        yourTable
    )
    SELECT
      *
    FROM
      sequenced
    WHERE
      sequence_id = 1
    

    With this technique you will return all the values in the table, rather than just the PrimaryKey and the search value.

    EDIT:

    The best way of dealing with that is to have another table that has the order of preference for each type.

     type | preference_value
    ------+------------------
      2   |         1
      3   |         2
      1   |         3
    

    This states that 2 is the most preferential, 3 is the second preference, and 1 is the third preference.

    Because it’s such a small group, however, it can also be done just with a simple CASE statement. See my amended query above.

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

Sidebar

Related Questions

I have a table, a simple table, only has 3 fields. ID Type Date
1I have the following two tables (sample data) and need to be able to
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 need to retrieve data (vehicle details, in this case) from a MySQL table
We have a simple Table per Type Entity Framework 4.0 model :- ALl classes
I have a very simple query (three where conditions; two equals, one between) from
This question is related to this one . I have a page table with
I have a table with one column that is of text type. There's a
I have the following sample of data to insert into tables(from parent to child,

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.