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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:11:39+00:00 2026-06-03T03:11:39+00:00

I am having a problem with a database join query and I am looking

  • 0

I am having a problem with a database join query and I am looking for someone to help me out.

Basically I’ve got two tables, Invoices and Receipts.

Invoices

Invoice ID
Amount
Date_Added

Receipts

ReceiptID
InvoiceID
Amount
Date_Added

The thing is that I need to produce a table like below, but I have multiple records in Receipts and I am pretty sure that the data is stored in a good way, just not exactly sure what the query would be.

InvoiceID   RecieptID    Amount     Balance     Date_Added
1           0            100.00     100.00      01.05.2012
1           1            100.00     0.00        02.05.2012
2           0            250.00     250.00      03.05.2012
3           0            100.00     350.00      04.05.2012
2           2            100.00     250.00      05.05.2012

Does this make sense? So it should be in date order. So effectively I can see line by line what is going on each date.

  • 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-03T03:11:40+00:00Added an answer on June 3, 2026 at 3:11 am

    Setup:

    USE tempdb;
    GO
    
    CREATE TABLE dbo.Invoices
    (
      InvoiceID INT,
      Amount DECIMAL(12,2),
      DateAdded SMALLDATETIME
    );
    
    CREATE TABLE dbo.Receipts
    (
      ReceiptID INT,
      InvoiceID INT,
      Amount DECIMAL(12,2),
      DateAdded SMALLDATETIME
    );
    
    SET NOCOUNT ON;
    
    INSERT dbo.Invoices SELECT 1, 100, '20120501'
    UNION ALL SELECT 2, 250, '20120503'
    UNION ALL SELECT 3, 100, '20120504';
    
    INSERT dbo.Receipts SELECT 1, 1, 100, '20120502'
    UNION ALL SELECT 2, 2, 100, '20120505';
    

    Query:

    ;WITH x AS 
    (
      SELECT InvoiceID, ReceiptID, Amount, DateAdded, 
       rn = ROW_NUMBER() OVER (ORDER BY DateAdded)
      FROM
      (
        SELECT InvoiceID, ReceiptID = 0, Amount, DateAdded
        FROM dbo.Invoices -- where clause?
        UNION ALL 
        SELECT InvoiceID, ReceiptID, Amount, DateAdded
        FROM dbo.Receipts -- where clause?
      ) AS y
    ),
    z AS
    (
      SELECT xrn = x.rn, x.InvoiceID, x.ReceiptID, x.Amount, x.DateAdded, 
        PlusMinus = CASE WHEN x.ReceiptID > 0 THEN -x.Amount ELSE x.Amount END
        FROM x LEFT OUTER JOIN x AS x2
        ON x.rn = x2.rn + 1
    )
    SELECT InvoiceID, ReceiptID, Balance = (
      SELECT SUM(COALESCE(PlusMinus, Amount)) 
      FROM z AS z2 
      WHERE z2.xrn <= z.xrn
    ), Amount, DateAdded
    FROM z
    ORDER BY DateAdded;
    

    Cleanup:

    DROP TABLE dbo.Invoices, dbo.Receipts;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got a SQL query running a FULL JOIN on multiple database tables. The
I am facing problem with Inner join in Access Database. I have two table
Sir, I am having some problem regarding database in SQL Server. My project is
i'm having a problem with my database. I have the folowing: create table book
I'm having a problem getting access to a database which lives on a remote
I'm having a problem creating a trigger on a database for a project I've
I'm having a problem in mapping an existing data class member to the database..
For some reason I'm having a problem retrieving data from my database. It leaves
I want to get the results of a left join between two tables, with
I'm trying to perform a select query over two tables, one containing, for example,

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.