• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 268
  • Last Modified:

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

0
kenuk110
Asked:
kenuk110
  • 2
  • 2
1 Solution
 
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
 
kenuk110Author Commented:
Brilliant, thank you so much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now