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
Solved

SQL Superset subset

Posted on 2011-09-20
12
833 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
  • 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
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 500 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 69

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL - format decimal in a string 5 39
Procedure syntax 5 39
Access join syntax when converting to T-SQL query 4 34
SQL Database Restore 2008 R2 1 13
I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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