Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2006, 10:19 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default Difference between recorsets

Hi

I have 2 recordsets with the same fields-

Name, ID, Year_ID, Week_ID

No PK the 3 ID's make up the unique record.

How can I just return the difference bewteen the 2 recordsets?

ANdy

 
Old August 2nd, 2006, 12:50 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

-- prepare test data
declare @table1 table (Name varchar(10), ID int, Year_ID int, Week_ID int)

insert @table1
select 'Peso 1', 1, 1, 1 union all
select 'Peso 2', 2, 1, 5

declare @table2 table (Name varchar(10), ID int, Year_ID int, Week_ID int)

insert @table2
select 'Jennie A', 1, 1, 1 union all
select 'Jennie B', 2, 1, 2

-- do the work
SELECT CASE
            WHEN t1.id = t2.id THEN 'Equal rows'
            WHEN t1.id IS NULL THEN 'Only in table 2'
            WHEN t2.id IS NULL THEN 'Only in table 1'
        END Uniqueness,
        ISNULL(t1.id, t2.id) id,
        ISNULL(t1.year_id, t2.year_id) year_id,
        ISNULL(t1.week_id, t2.week_id) week_id
FROM @table1 t1
FULL JOIN @table2 t2 ON t2.id = t1.id and t2.year_id = t1.year_id and t2.week_id = t1.week_id
ORDER BY 1, 2, 3, 4

 
Old August 4th, 2006, 07:07 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks I'll give this a try.

What I'm doing now is concatenating all the ID's to give me 1 id in each recordset and performing a NOT IN. Seems to work but not the most efficient I'll grant you.

Andy

 
Old August 4th, 2006, 07:27 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:Originally posted by aware


How can I just return the difference bewteen the 2 recordsets?
Just so we're clear, what exactly do you mean by the 'difference' between those two sets?

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old August 8th, 2006, 04:03 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi
I have 2 recordsets, 1 contains a list of suppliers to be rated, and the other a list of those already rated.

I want a list of those that should be rated but nave no rating, ie the difference between the 2 recordsets.

Using 'NOT IN' works OK, but the problem I have is that there is no PK on the data, only compound keys, so what I have done is concatenate the 3 id's to give a PK that I can use on the NOT IN.

Is there a way to use the NOT IN with mor than 1 id field?

Thanks

Andy

 
Old August 11th, 2006, 11:19 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi,

Sorry for my delay in responding, but the customer comes first. :)

As I understand your requirement, you are looking for the entries in one table which do not have corresponding entries in another. There is more than one way to do this.

One way is via an OUTER JOIN:
Code:
SELECT T1.Name, T1.ID, T1.Year_ID, T1.Week_ID
  FROM Table1 T1
  LEFT JOIN Table2 T2 ON T1.ID = T2.ID
                        AND T1.Year_ID = T2.Year_ID
                        AND T1.Week_ID = T2.Week_ID
 WHERE T2.ID IS NULL
   AND T2.Year_ID IS NULL
   AND T2.Week_ID IS NULL
This works because the LEFT JOIN on the two tables will result in NULL in the result for the column values in Table2 where there are no rows matching the JOIN condition in Table2.

From that result, we select only those rows whose Table2 values are NULL, meaning there is no Table2 row with those ID values.

Another way to do this is via a correlated subquery, using the NOT EXISTS test to select rows in Table1 which have no matching rows in Table2:
Code:
SELECT T1.Name, T1.ID, T1.Year_ID, T1.Week_ID
  FROM Table1 T1
 WHERE NOT EXISTS(SELECT * FROM Table2 T2
                    WHERE T2.ID = T1.ID
                      AND T2.Year_ID = T1.Year_ID
                      AND T2.Week_ID = T1.Week_ID)
Here we attempt to select a matching row in Table2 using the Table1 values and then select values from Table1 where the result of the attempted match has no rows, i.e. no matching rows in Table2.

I can't say which way is "better" - indeed, it's quite possible there would be no difference in the execution plans...

BTW, The fact that you have no "primary key" (a composite key is a perfectly good key) really has nothing to do with it, and wouldn't really help even if you had one, except to make the JOIN condition a little easier to write (maybe).

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old August 18th, 2006, 07:54 AM
Authorized User
 
Join Date: Jun 2003
Posts: 59
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Brilliant thanks, this is just what I need.

I'll let you know how Iget on.

Andy






Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between name(*) and name() vikkiefd XSLT 3 March 11th, 2008 06:40 AM
Difference ashu .NET Framework 1.x 1 March 27th, 2007 09:46 AM
Difference between / vs ./ vs ../ arulkumar ASP.NET 2.0 Basics 1 February 21st, 2007 12:19 PM
what's the difference? jdkxxx Oracle 3 October 8th, 2005 12:33 PM
The difference mhergenrader J2EE 1 July 15th, 2005 07:20 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.