Solved

SQL task for adding data not in table

Posted on 2011-09-20
6
241 Views
Last Modified: 2012-05-12
Hi all.

I have a SQL 2000 DTS that I would like to insert data present in one table not in a second table.

The first table (myTableA) has one field: ItemCategory

The second table (myTableB) has 2 fields: CustomerID and ItemCategory

I want to be able to add values from the first table (myTableA) that are not present in myTableB.

For example:

myTableA: AAA, ABC, DEF
myTableB: CustomerID: 123, ItemCategory: AAA
CustomerID: 123, ItemCategory: ABC

So the task would run and find that CustomerID: 123 was missing ItemCategory DEF

Any idea how I can do this?

Thank you in advance!
0
Comment
Question by:printmedia
[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
  • 3
6 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567369
for a single customer:
select * from mytableA a
where not exists( select null from myTableB b where b.customerid = 123 and b.itemcategory = a.itemcategory ) 

Open in new window



0
 

Author Comment

by:printmedia
ID: 36567382
How about for mutlitple customers?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 36567399
supposing you have a table with the customers:
select c.customerid, a.itemcategory
from mytableA a
cross join tblCustomer c
where not exists( select null from myTableB b where b.customerid = c.customerid and b.itemcategory = a.itemcategory )

Open in new window

0
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 

Author Comment

by:printmedia
ID: 36567431
Although we do have a separate customer table, the customers in myTableB are specific customers that do not include all our customers. The table that will hold the customers is myTableB.

The SQL task that runs before this one, adds new customers to myTableB so by the time this task runs, myTableB will have an updated customer list.

This task's purpose is to add new itemcategories (a new itemcategory is added to myTableA) to the existing list of customers in myTableB.

0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 36567459
we can use the same logic, nevertheless:
select c.customerid, a.itemcategory
from mytableA a
cross join (select customerid from mytableB group by customerid ) c
where not exists( select null from myTableB b where b.customerid = c.customerid and b.itemcategory = a.itemcategory )

Open in new window

0
 

Author Closing Comment

by:printmedia
ID: 36567491
Great thanks!
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

752 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