Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8819891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:28:31+00:00 2026-06-14T05:28:31+00:00

I need to perform a query whereby I can extract certain document entry numbers

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:28:33+00:00Added an answer on June 14, 2026 at 5:28 am

    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:

    ;with a as
    (
    select row_number() over(partition by JrnlMemo order by DocEntry) rn,
    count(*) over (partition by JrnlMemo) cnt
    , DocEntry DocEntryCN, Comments, JrnlMemo, DocTotal from @ORIN
    FROM ORIN WHERE DocDate BETWEEN '2012-10-27' AND '2012-10-29' AND Comments IS NOT NULL 
    ), b as
    (
    select JrnlMemo, count(*)-2 count 
    from oinv
    WHERE DocDate BETWEEN '2012-10-27' AND '2012-10-29' AND Comments IS NOT NULL 
    group by JrnlMemo
    )
    select a.DocEntryCn, a.Comments, a.JrnlMemo, a.DocTotal
    from a join b on a.cnt > a.rn + b.count and a.JrnlMemo = b.JrnlMemo
    order by DocEntryCn
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i need to perform a insert query for multiple rows whereby the first column
I need to perform this Oracle query in SQL Server: select case_id, channel_index, min(su_min)
I need to perform validation based on SQL query result. Query is defined as
I am writing an SQL query in which that I will need to perform
I need a query to perform the following: find the countryID where country =
Suppose we need to check three boolean conditions to perform a select query. Let
I need to use YQL (Yahoo Query Language) to perform a cross domain ajax
I need to perform a query 2.5 million times. This query generates some rows
I want to perform a query whereby I want to check whether on the
The following query will give me the list of IDs I need to perform

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.