Solved

SQL task for adding data not in table

Posted on 2011-09-20
6
235 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SQL server 2008 SP4 29 33
SQL Script to find duplicates 16 19
combine an MS SQL string in Idera DM 9 31
Copy Database Wizard Error 3 20
Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now