Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Trying to get overlap data from the same table

Posted on 2011-03-16
4
Medium Priority
?
370 Views
Last Modified: 2012-05-11
I have a product master table that has data as follows

product name  product type
A                     Type A
A                      Type B
A                      Type C
B                      Type B

In the above case I want a sql to just give me overlap of product type for distinct product name in other words only the products that has type overlap

product             product type
A                       Type B
B                        Type B

I know self join can help but sometimes if the table is too large it is getting time to get results - want to get the thoughts -
0
Comment
Question by:mahjag
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 11

Expert Comment

by:Akenathon
ID: 35152549
select * from product_master_table where product_type in
(select product_type from product_master_table group by product_type having count(*)>1)

If it's slow, check that you have an index which starts by the field product_type.
0
 
LVL 74

Accepted Solution

by:
sdstuber earned 500 total points
ID: 35154011
you right self join or IN or EXISTS clauses are slow because they all require a double hit on the same table

using analytics lets you query the table only once

SELECT product_name, product_type
  FROM (SELECT product_name,
               product_type,
               COUNT(*) OVER (PARTITION BY product_type) cnt
          FROM product_master_table)
 WHERE cnt > 1
0
 
LVL 11

Expert Comment

by:Akenathon
ID: 35158754
Good one! Avoiding the double hit ought to be better... I just wonder how much, and whether the CBO is smart enough to choose an index on product_type without further help (i.e. no hinting, no where predicates, nothing but the analytics clause)

mahjag: Can you share both queries' elapsed time? How big is your table?
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 35158826
If there is an index usage then it will be index search plus index lookup to table.  Or, just index lookup to table.  So, with or without index. Analytics "should" be faster. For very small tables it might not be noticable.  


But I definitely agree with the idea that both options should be tested.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to Unravel a Tricky Query Introduction If you browse through the Oracle zones or any of the other database-related zones you'll come across some complicated solutions and sometimes you'll just have to wonder how anyone came up with them.  …
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines
This video shows how to Export data from an Oracle database using the Original Export Utility.  The corresponding Import utility, which works the same way is referenced, but not demonstrated.

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question