Solved

Intersection and minus in MSSQL

Posted on 2004-04-22
3
7,274 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

930 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

13 Experts available now in Live!

Get 1:1 Help Now