Solved

Intersection and minus in MSSQL

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Display Date and Time 7 48
export sql server dbs 2 26
Create a Calendar table 29 43
Query Task 8 14
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
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.

734 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