Solved

SQL task for adding data not in table

Posted on 2011-09-20
6
238 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
  • 3
  • 3
6 Comments
 
LVL 142

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 142

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 

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 142

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

831 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