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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T18:09:42+00:00 2026-05-23T18:09:42+00:00

I have two tables: contacts: id, name 1 Alex 2 John documents: id, contactID,

  • 0

I have two tables:

contacts:

id, name
1   Alex
2   John

documents:

id, contactID, type
1     1         1
2     1         2
...
30    1         3
31    2         1
32    2         3

I want to get contact name, and document type. the only record for each contact. Contact may have several document types (the only document per type), and I have the following priority for document types: 2, 3, 1.
For example, Alex has documents of all types, but I should get only:

Alex, 2

John has document types = 1, 3, as a result I should get:

John, 3

Oracle database. But if you’ll be able to give sql standard solution, it would be great

  • 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-23T18:09:42+00:00Added an answer on May 23, 2026 at 6:09 pm

    Oracle 9i+, use:

    WITH example AS (
      SELECT c.name,
             d.type,
             ROW_NUMBER() OVER (PARTITION BY c.id
                                    ORDER BY CASE d.type
                                               WHEN 2 THEN 1
                                               WHEN 3 THEN 2
                                               WHEN 1 THEN 3
                                               ELSE 4
                                             END) AS rnk    
        FROM CONTACTS c
        JOIN DOCUMENTS d ON d.contactid = c.id)
    SELECT e.name, e.type
      FROM example e
     WHERE e.rnk = 1
    

    …or the non-Subquery Factoring (AKA CTE) version (still 9i+):

    SELECT e.name, e.type
      FROM (SELECT c.name,
                   d.type,
                   ROW_NUMBER() OVER (PARTITION BY c.id
                                          ORDER BY CASE d.type
                                                     WHEN 2 THEN 1
                                                     WHEN 3 THEN 2
                                                     WHEN 1 THEN 3
                                                     ELSE 4
                                                   END) AS rnk    
              FROM CONTACTS c
              JOIN DOCUMENTS d ON d.contactid = c.id) e
     WHERE e.rnk = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two database tables Contact (Id, Name, ...) and ContactOperationalPlaces (ContactId, MunicipalityId), where
I have two tables Employee (EmpID, Name, UpdatedBy, UpdateDate) EmpContact (ContactID, Contact,EmpID) I am
I have two tables: Contact (id,name) Link (id, contact_id, source_id) I have the following
I have two tables points and contacts and I'm trying to get the average
I have two tables on a webform. On a button click I want to
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables. Contacts table - contact_id, contact_email, contact_name, etc Opt out table
I have the following problem: I want to compare two tables and see if
So I have these two tables in my database: Companies -------------------- CompanyID (PK) Name
I have two tables: contacts and contact_temps. The contact_temps table mirrors the contacts table.

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.