Solved

Intersection and minus in MSSQL

Posted on 2004-04-22
3
7,271 Views
Last Modified: 2007-12-19
I have nearly 50 pairs of tables. Each table has nearly 20 columns. I would like to find out the number of rows that are common in each pair of tables. I also would like to findout the count of minus operation. I cannot use EXISTS and NOT EXISTS, because there are 20 columns, and 50 tables. I know that there is no direct operator for intersection, and minus in MSSQL. Is there any hacky way to findout this without writing complex program.
0
Comment
Question by:tnagasatish
3 Comments
 
LVL 26

Expert Comment

by:Hilaire
ID: 10897005
>>I cannot use EXISTS and NOT EXISTS, because there are 20 columns, and 50 tables.<<
Are you telling us that there's no primary key / unique indexes defined on your tables ?

To perform INTERSECT :
make 2 sub queries and make an inner join between the two

To perform Minus
make 2 sub queries and make a full outer join between the two, filtering to keep only
rows from a where b.column is null
and
rows from b where a.column is null

Don't think of anything else for the moment ...

Hilaire
0
 
LVL 50

Expert Comment

by:Lowfatspread
ID: 10897160
i agree with hilaire...

you could always generate the sql for the tables by reference to information schema columns/tables

have you considered

Select x.*
From (
select 'tab1' as tabnam,a.* from table1 as  A
union
select 'tab2' as tabnam,b.* from table2 as  b
) as x
Group by the list of 20 columns
having count(distinct Tabnam) = 2

would find the duplicates  



0
 
LVL 1

Accepted Solution

by:
ramakrishnadasa earned 500 total points
ID: 10897233
>>I would like to find out the number of rows that are common in each pair of tables<<

If you just want count, then you can do the following.

Intersection row count = row count of table1 + row count of table2 - number of distinct rows in table1 and table2.

You can easily find the row count of individual tables. For the number of distinct rows, you can do the following.

select count(*) from (
       select * from table1
       union
       select * from table2) t

By this formula, you can find out the intersection count, without specifiying all the fields.

For table1 - table2,
minus count = row count of table1 - intersection row count.

Hope this helps,
Rama Krishna.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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

15 Experts available now in Live!

Get 1:1 Help Now