Solved

Trying to get overlap data from the same table

Posted on 2011-03-16
4
357 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
  • 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 73

Accepted Solution

by:
sdstuber earned 125 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 73

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

How to Create User-Defined Aggregates in Oracle Before we begin creating these things, what are user-defined aggregates?  They are a feature introduced in Oracle 9i that allows a developer to create his or her own functions like "SUM", "AVG", and…
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
This video shows syntax for various backup options while discussing how the different basic backup types work.  It explains how to take full backups, incremental level 0 backups, incremental level 1 backups in both differential and cumulative mode a…
Via a live example, show how to take different types of Oracle backups using RMAN.

912 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now