Solved

Intersection and minus in MSSQL

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

856 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