Oracle Select Stmt

Greeting,

I have data like the following.

table_name          column_name
TBL1                     Name1
TBL1                     Name2
TBL2                     Name1
TBL2                     Name2

I need a SQL select stmt which gives me table_name which have column_name equals both Name1 and Name2.

Please suggest.

Thanks.
mrongAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

johnsoneSenior Oracle DBACommented:
SELECT table_name 
FROM   my_tab a 
WHERE  column_name = 'Name1' 
       AND EXISTS (SELECT 1 
                   FROM   my_tab b 
                   WHERE  a.table_name = b.table_name 
                          AND column_name = 'Name2') 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Alexander Eßer [Alex140181]Software DeveloperCommented:
The description of your problem is quite vague, but maybe you search for one of these:

select a.table_name
  from your_table b,
       your_table a
 where b.table_name <> a.table_name
   and b.column_name = a.column_name;

select a.table_name
  from your_table a
 where exists (select 'X'
          from your_table b
         where b.table_name <> a.table_name
           and b.column_name = a.column_name);

Open in new window

0
sdstuberCommented:
no need to query the table twice

SELECT table_name
    FROM yourtable
GROUP BY table_name
  HAVING COUNT(CASE WHEN column_name = 'Name1' THEN 1 END) > 0
     AND COUNT(CASE WHEN column_name = 'Name2' THEN 1 END) > 0
0
PortletPaulfreelancerCommented:
This would list table_name when it has both Name1 and Name2 as column_names in a single pass
SELECT
TABLE_NAME
FROM YOURTABLE
WHERE COLUMN_NAME IN ('Name1','Name2')
GROUP BY TABLE_NAME
HAVING MIN(COLUMN_NAME) = 'Name1'
AND MAX(COLUMN_NAME) = 'Name2'

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.