Solved

Trying to get overlap data from the same table

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pl/sql - query very slow 26 89
why truncate is faster than delete in oracle ? 4 51
running myfile.SQL from command line SQLPLUS 12c does not exit. 7 37
how to tune the query 17 61
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…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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.
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.

730 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