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 8734161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:47:10+00:00 2026-06-13T09:47:10+00:00

I have a table with fields TransactionID , Amount and ParentTransactionID The transactions can

  • 0

I have a table with fields TransactionID, Amount and ParentTransactionID
The transactions can be cancelled so a new entry posted with amount and ParentTransactionID as cancelled TransactionID.

Lets say a transaction

1 100 NULL

I cancelled the above entry, it will like

2 -100 1

Again cancelled the above transaction, so it should like

3 100 2

When I fetch I should get the record 3 as ID 1 and 2 got cancelled.
result should be

3 100 2

If I cancelled the 3rd entry no records should return.

SELECT * FROM Transaction t
WHERE NOT EXISTS (SELECT TOP 1 NULL FROM Transaction pt
WHERE (pt.ParentTransactionID = t.TransactionID OR t.ParentTransactionID = pt.TransactionID)
AND ABS(t.Amount) = ABS(pt.Amount))

This works if only one level of cancel is made.

  • 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-13T09:47:11+00:00Added an answer on June 13, 2026 at 9:47 am

    If all transactions are cancelled by a new transaction setting ParentTransactionId to the transaction it cancels, it can be done using a simple LEFT JOIN;

    SELECT t1.* FROM Transactions t1
    LEFT JOIN Transactions t2
      ON t1.TransactionId = t2.ParentTransactionId
    WHERE t2.TransactionId IS NULL;
    

    t1 being the transaction we’re currently looking at and t2 being the possibly cancelling transaction. If there is no cancelling transaction (ie the TransactionId for t2 does not exist), return the row.

    I’m not sure about your last statement though, If I cancelled the 3rd entry no records should return.. How would you cancel #3 without adding a new transaction to the table? You may have some other condition for a cancel you’re not telling us about…?

    Simple SQLfiddle demo.

    EDIT: Since you don’t want cancelled transactions (or rather transactions with an odd number of cancellations), you need a quite a bit more complicated recursive query to figure out whether to show the last transaction or not;

    WITH ChangeLog(TransactionID, Amount, ParentTransactionID, 
                   IsCancel, OriginalTransactionID) AS
    (
      SELECT TransactionID, Amount, ParentTransactionID, 0, TransactionID
      FROM Transactions WHERE ParentTransactionID IS NULL
      UNION ALL
      SELECT t.TransactionID, t.Amount, t.ParentTransactionID, 
             1-c.IsCancel, c.OriginalTransactionID
      FROM Transactions t
      JOIN ChangeLog c ON c.TransactionID = t.ParentTransactionID
    ) 
    SELECT c1.TransactionID, c1.Amount, c1.ParentTransactionID
    FROM ChangeLog c1
    LEFT JOIN ChangeLog c2
      ON c1.TransactionID < c2.TransactionID
         AND c1.OriginalTransactionID = c2.OriginalTransactionID
    WHERE c2.TransactionID IS NULL AND c1.IsCancel=0
    

    This will, in your example with 3 transactions, show the last row, but if the last row is cancelled, it won’t return anything.

    Since SQLfiddle is up again, here is a fiddle to test with.

    A short explanation of the query may be in order even if a bit hard to do in a simple way; it defines a recursive “view”, ChangeLog that tracks cancels and the original transaction id from the original to the last transaction in a series (a series is all transactions with the same OriginalTransactionId). After that, it joins ChangeLog with itself to find the last entry (ie all transactions that don’t have a cancelling transaction). If the last entry found in a series is not a cancel (IsCancel=0) it will show up.

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

Sidebar

Related Questions

Let's say I have a table named transaction with transaction_date, deposit, withdrawal fields. There
I have one table called: Transaction. This table has the following fields: (ID,ProductName,Amount,Date) placed
i have payment table fields update reason and amount & total field are change
I have a database table that is full of transactions transactionIDs and datetime fields.
hEY ALL i have a table that shows transactions and their status. How can
I have my Table structure like this :: ATT_Table : Fields - Act_ID, Assigned_To_ID,
I have table A with fields user1 and user2 and table B with user3
I have a table with fields user_id and votes . The votes field is
I have a table the fields are Id1 , ID2 , Points1 , Points2
I have Access table with fields, in which there is some data and path

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.