Solved

Trying to get overlap data from the same table

Posted on 2011-03-16
4
355 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
levels for reporting 5 51
Oracle Database Upgrade 13 43
PL/SQL LOOP CURSOR 3 42
Oracle and DateTime math 6 16
Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Note: this article covers simple compression. Oracle introduced in version 11g release 2 a new feature called Advanced Compression which is not covered here. General principle of Oracle compression Oracle compression is a way of reducing the d…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
This video shows how to Export data from an Oracle database using the Datapump Export Utility.  The corresponding Datapump Import utility is also discussed and demonstrated.

744 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

10 Experts available now in Live!

Get 1:1 Help Now