I need to perform a query whereby I can extract certain document entry numbers for deletion. (SQL Server 2008)
The first query gives me the results from a table OINV.
SELECT DocEntry AS "DocEntryINV" , Comments, DocTotal
FROM OINV WHERE DocDate BETWEEN '10-27-2012' AND '10-29-2012' AND Comments IS NOT NULL
ORDER BY DocTotal ASC, Comments
The second from a table ORIN
SELECT DocEntry AS "DocEntryCN" , Comments, JrnlMemo, DocTotal
FROM ORIN WHERE DocDate BETWEEN '10-27-2012' AND '10-29-2012' AND Comments IS NOT NULL
ORDER BY DocTotal ASC, Comments
OINV sample results
DocEntryINV JrnlMemo DocTotal
1 kaka-19 500
3 kaka-19 500
5 kaka-19 500
6 kaka-19 500
7 Rob-23 750
9 Rob-23 750
10 Alex-09 1000
11 Olma-08 1150
12 Paul-17 1250
13 Paul-17 1250
16 Paul-17 1250
17 Rita-99 1300
19 Rita-99 1300
ORIN sample results
DocEntryCN Comments JrnlMemo DocTotal
67 reverse kaka-19 500
69 reverse kaka-19 500
70 reverse kaka-19 500
71 reverse kaka-19 500
74 reverse kaka-19 500
75 reverse Rob-23 750
77 reverse Rob-23 750
78 reverse Rob-23 750
79 reverse Rob-23 750
84 reverse Paul-17 1250
86 reverse Paul-17 1250
87 reverse Paul-17 1250
For the table OINV, the duplicates arose from a point of sale software that pushed duplicates of invoices to an ERP database table, OINV. A program meant to correct the situation by creating credit notes to nullify Invoices didn’t work well, sometimes creating more credit notes than were invoices in the first place. (Invoices number 45,000+ in total). Sometimes credit notes were not created where needed for invoice duplicates.
How can I create a query using both tables where I can be able to reverse the extra entries on the ORIN table, if they exist? I need the query to return the results of the
DocEntryCN numbers to be reversed such as the following results for the above scenario.
DocEntryCN Comments JrnlMemo DocTotal
67 reverse kaka-19 500
69 reverse kaka-19 500
75 reverse Rob-23 750
77 reverse Rob-23 750
78 reverse Rob-23 750
84 reverse Paul-17 1250
90 reverse Rita-99 1300
NB. Comments stores Invoice numbers that are unique. If any two comments in the OINV table are the same, it means there is a duplicate error. The JrnlMemo field in the ORIN table stores the OINV invoice numbers. For any invoice repetitions no. k, the correct number of credit notes to reverse should be k – 1.
You should be aware that Rita’s data does not exist in the ORIN sample, so it can’t be in the result. I hope this can solve your problem: