Solved

Trying to get overlap data from the same table

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

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Concat multi row values of a field in oracle 6 63
Schema creation in Oracle12c 6 45
Oracle Listener Not Starting 11 44
Oracle function to insert records? 15 37
Working with Network Access Control Lists in Oracle 11g (part 2) Part 1: http://www.e-e.com/A_8429.html Previously, I introduced the basics of network ACL's including how to create, delete and modify entries to allow and deny access.  For many…
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.  …
Via a live example, show how to restore a database from backup after a simulated disk failure using RMAN.
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.

786 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