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

  • Home
  • SEARCH
  • 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 3393492
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:03:25+00:00 2026-05-18T04:03:25+00:00

I’ve written a solution to problem using PL/SQL and SQL and I can’t help

  • 0

I’ve written a solution to problem using PL/SQL and SQL and I can’t help thinking that it could be done 100% in SQL but am I am struggling to get started.

Here is the structure of the two tables (If it helps, the scripts to create them are at the end of the question)

Table t1 (primary key is both columns displayed)

ID    TYPE
1     A
1     B
1     C

2     A
2     B

3     B

The Type column is a Foreign Key to table T2 which contains the following data:

Table t2 (primary key is Type)

Type    Desc
A       xx

B       xx

C       xx

So given the the data in T1 the result I need will be:

For ID 1 because it has all the types in the foreign key table I would return the literal “All”

For ID 2 because it has two types I would like to return “A & B” (note the separator)

And finally for ID 3 because it has one type I would like to return just “B”

As promised here are the scripts to create all the objects mentioned.

create table t2(type varchar2(1),
                description varchar2(100)
                )                
/

insert into t2
values ('A', 'xx')
/

insert into t2
values ('B', 'xx')
/

insert into t2
values ('C', 'xx')
/

alter table t2 add constraint t2_pk primary key (type)
/

create table t1 (id number(10),
                 type varchar2(1)
                 )
/

alter table t1 add constraint t1_pk primary key(id, type)
/

alter table t1 add constraint t1_fk foreign key (type) 
references t2(type)
/

insert into t1
values (1, 'A') 
/

insert into t1
values (1, 'B')
/

insert into t1
values (1, 'C')
/

insert into t1
values (2, 'A')
/

insert into t1
values (2, 'B')
/

insert into t1
values (3, 'B')
/
  • 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-18T04:03:26+00:00Added an answer on May 18, 2026 at 4:03 am

    Something like this should get you what you are looking for:

    select
        id,
        case
            when cnt = (select count(distinct type) from t2)
            then 'All'
            else ltrim(sys_connect_by_path(type,' & '),' &')
        end types   
    from (
        select
            t1.id,
            t2.type,
            count(*) over (partition by t1.id) cnt,
            row_number() over (partition by t1.id order by t2.type) rn
        from
            t1
            inner join t2
                on t2.type = t1.type
    )
    where
        rn = cnt
        start with rn = 1
        connect by prior id = id and prior rn = rn-1;
    

    I would give your question +10 if I could for posting your object / data creation script!

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

Sidebar

Related Questions

No related questions found

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.