Results 1 to 8 of 8
  1. #1
    Join Date
    Sep 2023
    Posts
    4

    Question Create column using previous value

    Hello.

    I want to create a "Balance" column, exactly like in excel, in which if TYPE = WITHDRAW, BALANCE = PREVIOUS BALANCE (from the same "BALANC" column) - VALUE FROM CASH, if TYPE = SUPPLY; PREVIOUS BALANCE (from the same "BALANCE" column) + CASH VALUE.


  2. #2
    Bob Fitz's Avatar
    Bob Fitz is offline Access Developer
    Windows 10 Access 2019
    Join Date
    May 2011
    Location
    Essex UK
    Posts
    3,544
    You will need a field (column) in the query by which the query can be sorted. Perhaps one that records the date and time of each transaction. Do you have such a field.

    BTW "Type" is a reserved work in Access and should not be used as field name.
    Last edited by Bob Fitz; 09-01-2023 at 03:28 AM. Reason: More Info
    If this helped, please click the star at the bottom left of this posting and add to my reputation . Many thanks.
    Bob Fitzpatrick

  3. #3
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    There is no concept in databases for Previous or Next without an order, and not possible reference a previous calculated value

    So you need to provide an order - might be datetime (date on it's own won't work unless you only ever have one record per day), might be a PK (but risk of issues if entries not entered in the correct order).

    but the principle would be to use a subquery - so your query might look like this if using datetime.

    Code:
    SELECT A.DateTime, A.TranType, A.TranValue, (SELECT sum(TranValue*iif(Trantype='SUPPLY',1, -1)) as BALANCE FROM tblTrans B WHERE B.DateTime<=A.DateTime) AS BALANCE
    FROM tblTrans A
    ORDER BY A.DateTime
    The subquery is this part

    (SELECT sum(TranValue*iif(Trantype='SUPPLY',1, -1)) as BALANCE FROM tblTrans B WHERE B.DateTime<=A.DateTime) AS BALANCE

  4. #4
    Join Date
    Sep 2023
    Posts
    4
    What about an index column? It is possible with this column? Because i have a lot of transactions in one day.

  5. #5
    Join Date
    Sep 2023
    Posts
    4
    Yes, i have an index column because i have a date and time column but i have more than one transaction per day.

  6. #6
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    If the index column provides the right order, use that

  7. #7
    Join Date
    Sep 2023
    Posts
    4
    Can you write me the correct code with the index column? I m begginer in Access and SQL

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    Can you write me the correct code with the index column?
    see post #3, just change names to suit

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 16
    Last Post: 05-04-2022, 04:14 AM
  2. Filtering based on result of previous column
    By jackjsmtih88 in forum Queries
    Replies: 9
    Last Post: 10-28-2018, 03:41 PM
  3. Create a 4 Column Report from a 2 column data
    By trident in forum Reports
    Replies: 4
    Last Post: 01-19-2017, 11:50 AM
  4. Replies: 9
    Last Post: 03-02-2015, 06:05 PM
  5. Calculate Percentage based on previous column
    By VictoriaAlbert in forum Queries
    Replies: 1
    Last Post: 08-13-2011, 01:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums