Display items missing from one table to another?

Hi,

I have two tables, one called MSL and the other called ITEMS. I need to see which products are appearing in the ITEMS table that are NOT in the MSL table and vice versa, which items are in the MSL table but do not appear in the ITEMS table. Is this possible? I have attached my table structures for both, the link between the tables is the upcCode field.
CREATE TABLE [dbo].[item](
	[pk] [bigint] IDENTITY(1,1) NOT NULL,
	[countryCode] [nvarchar](3) NOT NULL,
	[cityCode] [nvarchar](2) NOT NULL,
	[storeCode] [nvarchar](3) NOT NULL,
	[itemCode] [nvarchar](255) NOT NULL,
	[storeType] [nvarchar](1) NULL,
	[vendorCode] [nvarchar](3) NOT NULL,
	[upcCode] [nvarchar](30) NOT NULL,
	[rc] [nvarchar](3) NULL,
	[gridCodeX] [nvarchar](4) NOT NULL,
	[gridCodeY] [nvarchar](4) NULL,
	[itemDescription] [nvarchar](255) NULL,
	[facings] [nvarchar](3) NULL,
	[brand] [nvarchar](255) NULL,
 CONSTRAINT [PK_item] PRIMARY KEY CLUSTERED

Open in new window

CREATE TABLE [dbo].[msl](
	[upcCode] [nvarchar](30) NOT NULL,
	[vendorCode] [nvarchar](3) NOT NULL,
	[countryCode] [nvarchar](3) NOT NULL,
 CONSTRAINT [PK_msl_1] PRIMARY KEY CLUSTERED

Open in new window

kenuk110Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
In item but not msl:

SELECT i.*
FROM item i LEFT JOIN
    msl m ON i.upcCode = m.upcCode
WHERE m.upcCode IS NULL

Open in new window


In msl but not item:

SELECT m.*
FROM item i RIGHT JOIN
    msl m ON i.upcCode = m.upcCode
WHERE i.upcCode IS NULL

Open in new window

0
kenuk110Author Commented:
Thats brilliant. It worked. Do you know how I can group by upcCode so I get one record per upcCode for both queries? I know I didn't ask that and I'll ask another question if I need to.

Thanks or the fast response.
0
Patrick MatthewsCommented:
If you only need the upcCode column...

In item but not msl:

SELECT i.upcCode
FROM item i LEFT JOIN
    msl m ON i.upcCode = m.upcCode
WHERE m.upcCode IS NULL
GROUP BY i.upcCode

Open in new window


In msl but not item:

SELECT m.upcCode
FROM item i RIGHT JOIN
    msl m ON i.upcCode = m.upcCode
WHERE i.upcCode IS NULL
GROUP BY m.upcCode

Open in new window



If you need the other columns, how would you specify which row to return?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kenuk110Author Commented:
Brilliant, thank you so much.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.