473,386 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Delete unmatched records

I have two tables invoice and so_salesorderdetail1, I want to delete those records from invoice which are not in so_salesorderdetail1.

these tables don't have any unique key so I am trying to make a composite key. I wrote the following two different queries but it doesn't seem working.

1.
DELETE FROM INVOICE
WHERE NOT EXISTS
(SELECT SO_SalesOrderDetail1.SalesOrderNo, SO_SalesOrderDetail1.ItemCode
FROM SO_SalesOrderDetail1 INNER JOIN INVOICE ON (SO_SalesOrderDetail1.ItemCode = INVOICE.Item_Code) AND (SO_SalesOrderDetail1.SalesOrderNo = INVOICE.Sales_Order));

2.
DELETE INVOICE.*
FROM INVOICE
WHERE (((INVOICE.Sales_Order) Not In (SELECT SalesOrderNo from SO_SalesOrderDetail1)) AND ((INVOICE.Item_Code) Not In (Select ItemCode from SO_SalesOrderDetail1)));


Is there any other way I can delete unmatched records.


Mark
Feb 28 '10 #1
4 3806
Delerna
1,134 Expert 1GB
Lets see if I can say this in a way that makes sense
In your second query you are saying
if the [INVOICE].[Sales_Order] number doesn't exist in the [SO_SalesOrderDetail1] table then I might want to delete this record but I had better make another check first.

That other check is
if the [INVOICE].[Item_Code] on that missing [INVOICE].[Sales_Order]
isn't used on ANY other order in the [SO_SalesOrderDetail1] table
then delete the record.

It's hard to tell because you haven't provided much info but I don't think that is what you meant to say.

My gut feeling is that the Item_Code on a missing order would surely be used on one or more other orders and therefore your query will never delete anything.


Is it not enough just to check [Sales_Order] for non existance?
If you want to experiment make a backup copy of the table and experiment on that.
Also, maybe instead of just deleting them you should insert them into a deleted records table first? Especially if this is live data.

That way you can always put them back in the future if you discover one or more should not have been deleted
Feb 28 '10 #2
Thanks for your input Delerna...

I am new in this area...so any help is appreciated

Just checking on Sales_Order for non existence does not give me the expected results..as this is not a unique key…

Let me try to explain...

If there are 4 items i1,i2,i3,i4 under sales order ABC…

I will have 4 rows and sales order will be ABC for all 4 items..only way it can be differentiated is by combining item code and sales order


SalesOrder ItemCode
ABC i1
ABC i2
ABC i3
ABC i4

I want to compare INVOICE and SO_SalesOrderDetail1 table and want to delete those from INVOICE which are not in SO_SalesOrderDetail1

If i1 and i2 are shipped out from Sales Order ABC...It will not be there in SO_SalesOrderDetail1 and I want to delete only these two rows from INVOICE.

This select query gives me the result I want, but I could not make it delete query somehow.

SELECT INVOICE.*
FROM INVOICE LEFT JOIN SO_SalesOrderDetail1 ON (INVOICE.Item_Code = SO_SalesOrderDetail1.ItemCode) AND (INVOICE.Sales_Order = SO_SalesOrderDetail1.SalesOrderNo)
WHERE (((SO_SalesOrderDetail1.SalesOrderNo) Is Null) AND ((SO_SalesOrderDetail1.ItemCode) Is Null));
Mar 1 '10 #3
Delerna
1,134 Expert 1GB
From your sample data I see that the two fields are of some char type

so what about this
Expand|Select|Wrap|Line Numbers
  1. DELETE INVOICE.*
  2. FROM INVOICE
  3. WHERE INVOICE.Sales_Order & INVOICE.Item_Code 
  4. Not In (   SELECT SalesOrderNo & Item_Code 
  5.            from SO_SalesOrderDetail1);
  6.  
Mar 1 '10 #4
It worked :)

Thanks Delerna....
Mar 2 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Reply via newsgroup | last post by:
Folks, When performing an update in mysql (using PHP), can I find out how many records were matched? mysql_affected_rows() won't work... and I have the following problem that I thought I...
26
by: rkleiner | last post by:
Is there a regular expression to find the first unmatched right bracket in a string, if there is one? For example, "(1*(2+3))))".search(/regexp/) == 9 Thanks in advance.
2
by: Joe Del Medico | last post by:
I have two tables A & B and I want to delete all the records in A that are not in B. Can I do this in the query builder? It seems like a simple problem. I can easily find the records in A that...
2
by: Colleyville Alan | last post by:
I want to extract some records that are common to three tables, but not contained in the fourth. Following what I have see in the archives and also trying the unmatched records query wizard, if I...
8
by: paulwilliamsonremove | last post by:
Hi, Using Access 2003. I am trying to delete records from one table/query ("qryHistoryPersonIDs") if they don't exist in another table ("qryDonations"). But the SQL syntax I came up with is...
2
by: rfranzl | last post by:
Hello, I need some help, I have about 200 databases that are copies of an original database that has a similiar table in all of the databases, called "tblCodebook". What I am trying to do is to...
1
by: rfranzl | last post by:
Hello, I need some help, I have about 200 databases that are copies of an original database that has a similiar table in all of the databases, called "tblCodebook". What I am trying to do is to...
2
by: scolivas | last post by:
Is there a way to automate this process? I have a query that finds the "Drop Offs" by doing an unmatch query against the hard table and the live table. I want to automatically pull these...
1
by: AccessHunter | last post by:
Please Help.... I have a query with 2 tables, LegalFile and Cases. LegalFile has the field CaseID(Number Field) and Cases has CaseNbr(Text Field). I am trying to find entries that are in...
2
by: zufie | last post by:
Does MS Access utilize something like a backlash (\) to make the following SQL code work? (SQL uses a backlash (\) to make the following SQL code work). Here is my code: INSERT INTO...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.