SQL statement running balance query with previous balance taken into account

Hi Guys
I have a SQL statement which caclulates the running balance
for a list of transactions in a transactions table. This SQL
statement is as follows:
SELECT transID, debit, credit,
(SELECT SUM(debit-credit)
FROM transactions as D1
WHERE D1.transID <= D0.transID) AS balance
FROM transactions AS D0
The only problem I'm having is that I have to display
transactions between a particular date range. I have for eg.
transID, transDate, debit, credit fields in my db.
However the problem I'm having is that when the transaction
records are pulled out for the specified date range the balances
are only calculated for those records. I need someway of having a
balance b/f (brought forward) so that the selected records use that
as a 'starting' balance and then calculate the running balance as
normal.
Any easy solutions?
Many, many thanks for your help in advance.
All the best
Wesley

You'd need to determine the b/f then apply it against the
transaction records when they are pulled for a date range. So if
the date range is
Feb 1st, 2009 -> Feb 28th, 2009
You'd need to also get the previous balance in a separate
query, then use it to correctly display the running balance when
you output the date range.
I suppose a simpler way involving a little more DB work (and
possibly lots of coding) is to store the current balance as well
with each transaction; then when performing a date range you'd have
the balance as well and not have to calculate it on the fly.

Similar Messages

  • Run the query with previous weeks date automatically

    Hi Guyz,
    I need to  dynamically populate the previous weeks  date value every week,
    i.e Run the query every Monday automatically, with previous weeks date value .
    Is there any SAP Exit which I can use ??
    Regards,
    Ravi
    Edited by: Ravi Srinivas on Aug 4, 2009 1:40 PM
    Edited by: Ravi Srinivas on Aug 4, 2009 1:52 PM

    Ravi,
    If you are thinking of re-running the previous week's queries automatically in the front-end, then U. Tummers is correct.  You can create new columns alongside your existing query if you so wish and use your restrictions with an offset sepending on how far back you'd like to go.
    Best,
    Philips

  • Changing sql-statement before executing query

    I want to change the sql-statement of a query just before it is executed.
    I have tried it with a query redirector/sessionevent listener, but I could get it to work.
    Do you have any examples.
    I want to change the tablename in a sql-statement like this:
    before
    select id from emp
    after
    select id from emp_v
    Any examples out there to do this?

    ... I am using SAP NetWeaver BI 7.0
    Monique

  • I want to keep track of a running balance for a account

    I would like to keep a running balance for a account, I would like to be able to add a new amount whenever there is a new transaction and be shown what the new balance for that account is. I am sure that this is simple to do and i can do it the long way however a shortcut would be greatly appreciated, if there is already a thread that answers this question, please point me in the right direction.

    See this thread, among others.  "Running totals" is another way you might search.
    SG

  • I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year

    I need to add a single field from with_item table . need to write select query with reference to company code , account doc no , fiscal year

    Hi Arun ,
    Can you explain little bit more ??
    what is account doc no? 
    what are the transactions should be displayed in your output??
    -Rajesh N

  • Profiles are not taken into account with DIS

    Hi,
    I created several profiles in the content management.
    They work correctly.
    Now, I have to use Desktop Integration Suite.
    The installation worked correctly.
    The problem is, when a user saves and closes the document, the "check-in" screen is the standard one.
    So, all the metadata are displayed on the screen (nearly 40 - it is becoming a mess for the users to known which ones to fill).
    I would like to use the profiles to reduce the metadata (as it is done for the check-in and search in the content server).
    How can I do it ?
    Does anybody encounter this problem ?
    Any help is welcome.
    Kind regards
    Pierre

    Hi Jason,
    I understood my mistake (but I installed the patch).
    The profile is correctly taken into account.
    But the trigger of my profile is the document type.
    With the DIS, I don't know how to transfer metadata before to open the "standard check-in" page.
    Once the check-in page is opened, if I change the document type, the profile is not activated.
    Do you know if it is possible :
    - To transfer metadata to the content server that could be used to determine the profile, for example by defining properties on the Word document.
    - To change of profiles by changing the value of the trigger used for the profile.
    For the moment, the only trick I've found was to create a "server" by value of the trigger and to put the document type as a data to be remembered.
    At that moment, the profile is correctly loaded.
    But, this solution is not very user-friendy. Do you see another one ?
    Pierre

  • SQL Statement needed for Query Generator

    Dear Experts,
    My view has the following three columns.
    1, Start Date
    2. End Date
    3. Balance.
    I need an SQL statement to write in query generator, which has to check if EndDate is less than the Current date and balance is above zero. If so, it assumes the last date of the job is already over, outstanding materials not yet been returned. Hence it has to return 'Pending'. If EndDate is greater than current date, it means final date for completion of the process is yet to come. So the task is in progress, hence it has to return 'Progress'. If the balance is Zero, all materials connected with the task have been received either as a FG or as Raw Materials. So it has to return, 'Completed'. I need a singe SQL Statement for this.
    Please help me.
    Thanks in advance.
    Regards
    Anand

    Dear Anand,
    Try this one:
    CASE balance WHEN 0 THEN 'Completed' ELSE CASE WHEN DATEDIFF(DD, EndDate, GetDate()) >= 0 THEN 'Progress' ELSE 'Pending' END END
    I have assumed you don't have negative balance.  It that is not true, give me the complete case.
    Thanks,
    Gordon
    Thanks,
    Gordon

  • How to monitor the sql statement running?

    Hi ,
    i need to check the status of the sql script running on the database.
    what is the way to chect that?
    if the particular query is taking very long time what might be the reason?

    Since you are [relatively] new at this ..
    the recommended technique will change based on version of Oracle database, the edition of the database, the options of the database and the operating system.
    For example, I would normally suggest a newbie with Oracle Database 10g or 11g Enterprise Edition to use the Enterprise Manager Diagnostics Pack. A demo thereof is on the Oracle BY Example site (http://otn.oracle.com) - look for Tutorials towards the top.
    As for why ... it's waiting on things. Which things may depend on the database, the operating system, the disk subsystem, the network subsystem, memory allocation, sorting, other load, and even how well or badly the SQL statement was written.

  • Powershell - SQL statement for WMI query

    Need to input / update sql database, two main tables: server and diskDrive
    The Primary key for server table is server_id which is also a foreign key in diskDrive table.
    There is no primary key set in the diskDrive table.
    I am able to insert all of the basic data in the server table no problem. 
    What I need help creating/understanding is how to account for the servers with multiple drives.
    Here is my command for accounting for all drive information:
    $diskDrive = gwmi -query "select * from Win32_Volume where DriveType='3' AND DriveLetter IS NOT NULL" | Select @{Name="Device";Expression={$_.DriveLetter}},`
    @{Name="Capacity";Expression={[math]::round(($($_.Capacity)/1GB),2)}},`
    @{Name="FreeSpace";Expression={[math]::round(($($_.FreeSpace)/1GB),2)}},`
    @{Name="UsedSpace";Expression={[math]::round((($_.Capacity - $_.FreeSpace)/1GB),2)}},`
    @{Name="PercentFree";Expression={[math]::round(($($_.FreeSpace)/$($_.Capacity)*100),2)}}
    My thought is to create an exist statement to determine if I need to insert or update the Database:
    $ServerExist = "Select server_id from server where server_name = '$ServerInfo.csname'"
    if ($ServerExist) {
    update statement
    } else {
    #Create SQL Insert Statement with your values
    $insert_stmt = "INSERT INTO Server(server_name, domain_name, Manufacturer, Model, Memory)
    VALUES ('$($Output.Name)','$($Output.Domain)','$($Output.Manufacturer)','$($Output.Model)','$($Output.TotalPhysicalMemory)')" -replace "\s+"," "
    $cmd.CommandText = $insert_stmt
    ## Invoke the Insert statement
    $cmd.ExecuteNonQuery()
    I have tested the insert statement and that works fine.  However I have not implemented the $diskDrive aspect of the insert. 
    Although there will be a slight difference between the insert and update statement for the diskDrive table, still need to work out the iteration aspect for sql statement.
    Items that will need to be inserted/updated per drive:
    Server_id
    DriverLetter
    Capacity
    FreeSpace
    UsedSpace
    PercentFree
    Given my $diskDrive query, I think there needs to be While($diskDrive) statement that will iterate through the drive info and then insert/update until While is finished. 
    However I’m not sure how that is accomplished and hoping to get some help.
    Please let me know if any other information is needed.
    Thanks,
    Jr. Admin

    Hello jrv,
    The problem is my limited experience in powershell and how to iterate through the $diskDrive variable and pull the specific drive information including the server_id and inserting or updating the diskDrive table accordingly. 
    I was able to figure out how to do the insert for one specific wmi entry but in regards to the disk drive, i'm not sure how to work the scripting to run through each drive, insert/update and continue until all drives have been accounted for.
    Please let me know if you need any other information.  I will consider posting later on SQL forum.
    Thanks,
    Jr. Admin

  • How do I use SQL statements to perform calculations with form fields????

    Please help!!! I don't know how to use a SQL statement within my APEX form......
    My form is below. The user will enter the values in the form. Click on Submit. Then we need to run a SQL select statement with those values.
    Our form looks like this:
    Start_Date ____________
    Per_Period ____________
    Period ____________
    [Submit Button]
    The user will enter these 3 values in the form.
    This is an example of an user providing the values:
    Start_Date 03/14/08_______
    Per_Period $200.00________
    Period 4____________
    [Submit Button]
    Then they will click the Submit Button.
    The SQL statement (BELOW) returns output based on the users selections:
    START_DATE PER_PERIOD PERIOD
    14-MAR-2008 00:00 200 Week 1 of 4
    21-MAR-2008 00:00 200 Week 2 of 4
    28-MAR-2008 00:00 200 Week 3 of 4
    04-APR-2008 00:00 200 Week 4 of 4
    Total 800
    This is the full text of the SQL that makes the output above:
    with criteria as (select to_date('03/14/08', 'mm/dd/rr') as start_date,
    4 as periods,
    'Week' as period,
    200 per_period from dual),
    periods as (select 'Week' period, 7 days, 0 months from dual
    union all select 'BiWeek', 14, 0 from dual
    union all select 'Month', 0, 1 from dual
    union all select 'ByMonth', 0, 2 from dual
    union all select 'Quarter', 0, 3 from dual
    union all select 'Year', 0 , 12 from dual
    t1 as (
    select add_months(start_date,months*(level-1))+days*(level-1) start_date,
    per_period,
    c.period||' '||level||' of '||c.periods period
    from criteria c join periods p on c.period = p.period
    connect by level <= periods)
    select case grouping(start_date)
    when 1 then 'Total'
    else to_char(start_date)
    end start_date,
    sum(per_period) per_period,
    period
    from t1
    group by rollup ((start_date, period))
    THANKS VERY MUCH!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    You're just doing a parameterized report, where the input fields are your parameters.
    Check out the Advanced Tutorial titled Parameterized Report here:
    http://download.oracle.com/docs/cd/E10513_01/doc/appdev.310/e10497/rprt_query.htm#BGBEEBJA
    Good luck,
    Stew

  • PL/SQL report running slow even with Shared_Servers set to 10

    I am running into a problem with a PL/SQL query that displays in the Report region. I have changed my Shared_Servers parameter and that has not helped. There are less than 10 users on the system. I ran debug and the app is running a long time (10 seconds) in "rows loop: 20 row(s)" where 20 is the number of rows displayed per page. If I run the query in regular SQL, then the results are displayed almost instantaneously. Any ideas?

    I figured out my problem. My statistics were out-of-date on one of my tables. I re-analyzed the table and that solved the problem. Now the results display within milliseconds. What a huge difference that made.

  • Can I change sql statement for some query runtime?

    Hi All!
    The problem that I have to implement report with query like this: select * from a where b in (?,?,?). The number of parameters depends on user choice. On the client side it will be a list of check boxes. Is there any way to implement such report? I can see just one way so far - to modify sql statement runtime.
    Thanks!

    hello,
    create a user-parameter myListe or myWhere (wee examples below) and change your query to
    select ... from ... where b in (&<myList>)
    or even better
    select ... from ... &<myWhere>
    now you can pass in the constructed list or where clause. you can also construct the values in the afterParameterform-Trigger
    regards,
    philipp

  • How to get SQL statement from (Collection) query.execute();

    We have a JDO Persistence class ClassName that execute query using following code. After this statement get executed, record does not get updated in the DB. Is there a way to check what SQL statement got submitted in the (Collection) query.execute(); ??? <br><br>
    Extent extent = pm.getExtent(ClassName.class, false); <br>
    Query query = null; <br>
    try { <br>
    query = pm.newQuery(extent, filter); <br>
    Collection results = (Collection) query.execute();<br>
    Iterator i = results.iterator();<br>
    if (i.hasNext()) {<br>
    ...<br>
    }<br>

    You can always find out the names of tables that are views, using java.sql.DatabaseMetaData and its getTables() method.
    This tends to be a nice source of examples:
    javaalmanac.com
    However, if you're asking for the underlying SQL used to CREATE VIEW, I don't see anything in the API that will give you that. After all, JDBC shouldn't have to know if it's dealing with an ordinary table or a view. I think you'd have to ask your DBA for the underlying SQL.
    Once you have it, what do you plan to do with it?
    %

  • Ora-03114 running a query with group by

    Hi, I've a query with a group by on a sub-query, something like
    SELECT <40+ fields>
      FROM (SELECT <40+ fields>
              FROM table
    GROUP BY <40+ fields> I don't have any problem running this query directly via toad. This query is a cursor in a procedure in a package and, if I invoke it on the same data, the session crash "with ora-03114 not connected to oracle". If I modify the query selecting less fields, 22, I don't have any problem while with 23 the crash appears.
    Furthermore, I don't have any problem in other databases with similar data.
    The db version is 9.2.0.6.0 - 64bit
    Any idea/advice?
    Edited by: 912104 on 3-feb-2012 2.02
    Edited by: 912104 on 3-feb-2012 2.03
    Edited by: 912104 on 3-feb-2012 2.03

    912104 wrote:
    I have difficult to have more information, I can only add:
    select * from v$version
    BANNER
    Oracle9i Enterprise Edition Release 9.2.0.6.0 - 64bit Production
    PL/SQL Release 9.2.0.6.0 - Production
    CORE    9.2.0.6.0 ;   Production
    TNS for IBM/AIX RISC System/6000: Version 9.2.0.6.0 - Production
    NLSRTL Version 9.2.0.6.0 - Production Can I ask what do you think about the different behavior via toad and via package? I mean, it's possible that is a server bug?
    Edited by: 912104 on 3-feb-2012 4.02Are you retrieving the entire result set via toad? or just the first few records?
    Are there any TOAD non-fetched column values in your result set, i.e. CLOBS, XMLTypes, nested types?
    What does your code do with the cursor? Presumably you don't loop through it and do nothing. Are you sure it's the select that's causing the error, or something you are doing with the data in the procedure?
    Can you not adding some instrumentation to your code so that you know exactly what line/data values are processed at the time of the crash? Does it always crash on the same values/line or does it vary?
    "Furthermore, I don't have any problem in other databases with similar data." ... what do you mean by "similar data". You only need one odd value in one column to cause you a 3114 in the right circumstances. It seems like you are long way off establishing what exactly the problem is. It's always useful not to close doors to lines of thought. You need to systematically track down the root cause of the issue by exclusion and assumptions are the enemy of that process.

  • Oracle 8i sql statements running.

    hi,
    how i can know my sql statements is running or not in oracle 8i ?
    tks.

    Ah, is that what you meant...
    I thought you were wondering where or not a particular statement would compile or not on an Oracle 8i instance....
    http://download-west.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch3157.htm#10887
    http://download-west.oracle.com/docs/cd/A87860_01/doc/server.817/a76961/ch3136.htm#9225
    Both views exist in Oracle 8i....

Maybe you are looking for