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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T00:31:04+00:00 2026-05-19T00:31:04+00:00

Let’s say I have a table named transaction with transaction_date, deposit, withdrawal fields. There

  • 0

Let’s say I have a table named transaction with transaction_date, deposit, withdrawal fields. There may or may not be transaction on a day but can have multiple transactions for each day. So, what I need to do is given a date range, say December 1, 2010 to December 31, 2010, I need to figure out the minimum balance on each day. Let’s assume there are transactions before December 1, 2010 as well. Is there anyone who can give me an idea on this one?

Thank you.

Update With Example

 tran_date   withdraw    deposit
2010-11-23       0.00      50.00
2010-12-10       0.00      50.00
2010-12-10       0.00     200.00
2010-12-12     100.00       0.00
2010-12-20       0.00      50.00
2010-12-20      70.00       0.00
2010-12-20       0.00      50.00
2010-12-20       0.00      50.00
2010-12-24     150.00       0.00

In above example, the minimum daily balance from Dec 1 to Dec 10 would be 50. On Dec 10 there are two deposits totaling 70 but the minimum balance on that day would be 50 (carried over from previous day).

Now lets look at multiple transactions.

The carried over on Dec 20 is 200. The first deposit makes it 250, the second one makes it 180, the third one makes it 230 and the last transaction makes it 280. So, the minimum balance for that day would be 180 after withdrawing 70 on the second transaction of the day. Is it possible to generate this using a query on PostgreSQL 8.4 or should I use another approach?

  • 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-05-19T00:31:05+00:00Added an answer on May 19, 2026 at 12:31 am

    Edit2
    This is a full example, including the (minimum) balance from the previous day (as far as I can tell with such a small set of data). It should run on 8.4.

    I refactored the derived tables to use CTE (common table expression) to make it (hopefully) a bit more readable:

    WITH days AS (
       -- generate a liste of possible dates spanning 
       -- the whole interval of the transactions
       SELECT min(tran_date) + generate_series(0, max(tran_date) - min(tran_date)) AS some_date
       FROM transaction
    ),
    total_balance AS (
      -- Calculate the running totals for all transactions
      SELECT tran_id,
             days.some_date as tran_date, 
             deposit, 
             withdrawal,
             sum(deposit - withdrawal) 
                 OVER (ORDER BY some_date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) as balance
      FROM days
       LEFT JOIN transaction t ON t.tran_date = days.some_date 
    ),
    min_balance AS (
      -- calculate the minimum balance for each day 
      -- (the smalles balance will have a '1' in the column balance_rank)
      SELECT tran_id, 
             tran_date,
             rank() OVER (PARTITION BY tran_date ORDER BY balance) as balance_rank,
             balance
      FROM total_balance
    )
    -- Now get everything, including the balance for the previous day
    SELECT tran_id,
           tran_date,
           balance,
           lag(balance) over (order by tran_date) as previous_balance
    FROM min_balance
    WHERE balance_rank = 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have a link in a table like: <td class=ms-vb width=100%> <a
Let's say I have a table that looks something like this: ------------------------------- id|column2|column3 |column4
Let's say I'm building a data access layer for an application. Typically I have
Let's say we have a simple function defined in a pseudo language. List<Numbers> SortNumbers(List<Numbers>
Let's say I have a drive such as C:\ , and I want to
Let's say that we have an ARGB color: Color argb = Color.FromARGB(127, 69, 12,
Let's say that I have an arbitrary string like `A man + a plan
Let's say I have a class like this: public class Person { private String
Let's say I have this: public DefaultListModel model = new DefaultListModel(); how do i

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.