Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Superset subset

Posted on 2011-09-20
12
Medium Priority
?
923 Views
Last Modified: 2012-05-12
I have following table table with 3 millions records where I need help to find Superset Subset relationship.

SetPackage
--------------
SetID int
PackageName varchar(35)

Say I have inserted few sample rows like following:

INSERT into SetPackage  VALUES
(1,'A')
INSERT into SetPackage VALUES
(2,'A')
INSERT into SetPackage VALUES
(2,'B')
INSERT into SetPackage VALUES
(3,'A')
INSERT into SetPackage VALUES
(3,'B')
INSERT into SetPackage VALUES
(3,'C')
INSERT into SetPackage VALUES
(3,'E')
INSERT into SetPackage VALUES
(4,'B')
INSERT into SetPackage VALUES
(4,'C')

I want to know efficient way to know SuperSetID,SubSetID combination from this large amount of data.

I wrote following query but it takes long time and fills tempdb space:

SELECT A.SetID SupersetID, B.SetID SubsetID
FROM SetPackage A, SetPackage B
WHERE A.SetID <> B.SetID
  AND A.Package = B.Package
GROUP BY A.SetID, B.SetID
HAVING NOT EXISTS
(
    SELECT * FROM SetPackage C
    WHERE C.SetID = B.SetID
       AND NOT EXISTS (
        SELECT * FROM SetPackage D
        WHERE D.SetID = A.SetID
          AND D.Package = C.Package
        )
)
0
Comment
Question by:mayankvindhya
[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
  • 3
  • 2
  • 2
  • +2
12 Comments
 
LVL 25

Expert Comment

by:jogos
ID: 36569582
The filtering part in the HAVING could be in the WHERE. Why because they will be earlier filtered out so they won't be grouped anymore.  
Wonder also if its an explicit choise not to join C and D but writing them in a separate not exists.


0
 

Author Comment

by:mayankvindhya
ID: 36569616
Do you have better query?Please share.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 36569733
>>Do you have better query?<<
Have you checked the suggestion made?  Does it return the correct results?  Does it perform adequately?
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
LVL 74

Expert Comment

by:sdstuber
ID: 36569749
what would you want returned if the following rows are added?


INSERT INTO setpackage
VALUES (5, 'D');

INSERT INTO setpackage
VALUES (6, 'D');


each setid could be the superset of the other
0
 

Author Comment

by:mayankvindhya
ID: 36570383
I don't want set id 5,6 to be superset or subset as the values are same
0
 
LVL 74

Expert Comment

by:sdstuber
ID: 36570454
ok,  your query above returns both, that's why I sought confirmation thanks
0
 
LVL 25

Accepted Solution

by:
jogos earned 2000 total points
ID: 36571733
Seemed to me my sugestions did meet your sql scils to type out.

In example I did implemented my 2 sugestions at same time, but the join C/D could also be implemented in the HAVING-sollution
But in performance tuning it's always beter to measure (profiler) the situation before changement, make a first change and compare new measurement and result, make next change ...
So you won't end up making 10 changes don't know where your gain was or which of the 10 changes was the cause of an altered result.
SELECT A.SetID SupersetID, B.SetID SubsetID
FROM SetPackage A, SetPackage B
WHERE A.SetID <> B.SetID
  AND A.Package = B.Package
AND NOT EXISTS 
(
    SELECT * FROM SetPackage C
         inner join SetPackage D on  AND D.Package = C.Package
    WHERE C.SetID = B.SetID
       AND D.SetID = A.SetID
) 
GROUP BY A.SetID, B.SetID

Open in new window

0
 
LVL 25

Expert Comment

by:jogos
ID: 37196812
No feedback on suggestions from author on both theorecical as copy/past sollutions that can make a difference in time and tempdb-usage.
0
 
LVL 71

Expert Comment

by:Qlemo
ID: 37242424
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

715 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