Queries

N

Newby to Access

Could someone tell me how to write the query that will filter "duplicate
invoice numbers" in a table of 80k+ records and a filter for "same dollar
amounts" in the same table.

Thank you.

James
 
K

Ken Sheridan

James:

Are you saying you want to return rows where the invoice number AND the
amount is duplicated in one or more other rows? If so:

SELECT InvoiceNumber, Amount
FROM Invoices
GROUP BY InvoiceNumber, Amount
HAVING COUNT(*) > 1;

or are you saying you want to return rows where the invoice number OR the
amount is duplicated in one or more other rows? If so:

SELECT InvoiceNumber, Amount
FROM Invoices AS I1
WHERE
(SELECT COUNT(*)
FROM Invoices AS I2
WHERE I2.InvoiceNumber = I1.InvoiceNumber) > 1
OR
(SELECT COUNT(*)
FROM Invoices AS I2
WHERE I2.Amount = I1.Amount) > 1;

An alternative to the latter, and probably a more efficient solution, would
be the following, but this depends on the table having a uniquely valued
column such as InvoiceID. If you don't have this then adding an autonumber
column would give you the necessary column:

SELECT InvoiceNumber, Amount
FROM Invoices AS I1
WHERE EXISTS
(SELECT *
FROM Invoices As I2
WHERE (I2.InvoiceNumber = I1.InvoiceNumber
OR I2.Amount = I1.Amount)
AND I2.InvoiceID <> I1.InvoiceID);

Indexing theInvoiceID, InvoiceNumber and Amount columns should help
performance considerably.

Ken Sheridan
Stafford, England
 
J

John W. Vinson

On Mon, 27 Oct 2008 14:25:03 -0700, Newby to Access <Newby to
Could someone tell me how to write the query that will filter "duplicate
invoice numbers" in a table of 80k+ records and a filter for "same dollar
amounts" in the same table.

Thank you.

James

Not without a clearer explanation, no; a descripton of the structure of your
table and a clearer problem statement would help. Are these two separate
queries, or are you looking for duplicate invoice numbers and duplicate dollar
amounts in the same query?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top