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

  • SEARCH
  • Home
  • 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 8212585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:48:25+00:00 2026-06-07T10:48:25+00:00

I have a sql question that is closely related to this question – SQL

  • 0

I have a sql question that is closely related to this question – SQL – Need to find duplicate records but EXCLUDE reversed transactions

I need to remove all reversal “pairs” for a recordset using (if possible) non procedural SQL. The specific rdbms is Oracle 11g, but I would like the SQL to be as generic as possible so the same strategy can be used in SQL Server 2008. The example recordset looks like this:


 ROW     |    DATE    |    QTY    |    FUEL_TYPE    |    REVERSAL    |
1        | 01-MAY-12  |    23.3   |    DSL          |    N           |
2        | 01-MAY-12  |   -23.3   |    DSL          |    Y           |
3        | 01-MAY-12  |    23.3   |    DSL          |    N           |
4        | 01-MAY-12  |    23.3   |    DSL          |    N           |
5        | 01-MAY-12  |    23.3   |    DSL          |    N           |
6        | 01-MAY-12  |    18.6   |    DSL          |    N           |
7        | 01-MAY-12  |   -18.6   |    DSL          |    Y           |
8        | 01-MAY-12  |    14.9   |    GAS          |    N           |

The desired outcome of the query would reduce this recordset to:


 ROW     |    DATE    |    QTY    |    FUEL_TYPE    |    REVERSAL    |
3        | 01-MAY-12  |    23.3   |    DSL          |    N           |
4        | 01-MAY-12  |    23.3   |    DSL          |    N           |
5        | 01-MAY-12  |    23.3   |    DSL          |    N           |
8        | 01-MAY-12  |    14.9   |    GAS          |    N           |

Notice that duplicates are possible, but the reversal “pairs” always need to be removed.

edit
The rows and row numbers are irrelevant and are just used to illustrate. It doesn’t really matter which records are removed, just that there is always a “pair” – a positive amount and negative amount. So, for example, row 2 could be paired with 1,3,4 or 5 and removed.

Also, the logic that populates the table and the table structure itself is controlled by vendor software, and DOES NOT include the original id of the record that is being reversed in a reversal record. I don’t really have any control over this.
/edit

Incidentally, I would love it if the MINUS keyword were changed such that it functioned similar to UNION and UNION ALL – in that MINUS would remove only single rowsets that match from a second recordset, but MINUS ALL removed every row that matches values from a second recordset. If that were the case, this problem would be trivial (at least for the way that my brain thinks).

  • 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-07T10:48:27+00:00Added an answer on June 7, 2026 at 10:48 am

    It turns out I was looking at the problem in a pretty terrible way. Instead of finding the exact reversal pair, I just did a SUM with a GROUP BY, so only the values that I cared about keeping remained.

    The end result is that transactions will end up distinct – especially if, like in my case, the real transaction table is actually a datetime value instead of date.

    SELECT SUM(QTY) AS newQTY, DATE, FUEL_TYPE
    FROM fuel_transactions
    GROUP BY DATE, FUEL_TYPE
    

    The only time this won’t produce the values that you really want are if you have a need to maintain the id’s of the transactions, or if you have a situation in which multiple transactions occur at the exact same time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL question, related to this and this question (but different). Basically
I have a noobie question: I need to write SQL script that returns 3
I have another SQL/access 2007 question that seems really basic but I'm not sure
This may be a really basic SQL question, but I have seen a SQL
This is sort of SQL newbie question, I think, but here goes. I have
I have a SQL question that i need to ask the experts. To be
I have a short question. Im my current project I'm using LINQ-to-SQl. That is
I have an SQL question which may be basic to some but is confusing
I have this question in which I have a SQL Server Compact Edition database
I have two tables: users and user_depts. Let's say (for this question) that users

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.