473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Help with running totals

I have a query that takes monthly totals and rolls them up to get a
balance at a specific time code

eg:
dt bucket mon_ttl
--- ------ -------
199903 0192 -172527
199906 0192 546707
199909 0192 -278684
199910 0192 1100139
199911 0192 495279
199912 0192 -1690914
200003 0192 1025431
200006 0192 -1731678
200009 0192 1979256
200012 0192 -1273009

Rolls up like

DT BUCKET OPEN
--------- ---------- ----------
199903 0192 0
199906 0192 -172527
199909 0192 374180 (-172527+546707)
199910 0192 95496 etc...
199911 0192 1195635
199912 0192 1690914
200003 0192 0
200006 0192 1025431
200009 0192 -706247
200012 0192 1273009

I get that using

select dt, bucket, lag(run_ttl,1,0)
over (order by dt) open
from
(select dt, bucket, ttl,sum(ttl)
over (partition by bucket order by dt) run_ttl
from
mon_tot
where bucket='0192')

But I want to keep that last balance going forward...
eg: if I want the open as at 200301 and 200012 was the last non-null
balance
I want to carry that forward.
200012 0192 1273009
200101 0192 1273009
[..]
200301 0192 1273009

But I need to 'make up' the data going forward

I'm on 9.2.0.3 anyone have any tips/ideas?
Jul 19 '05 #1
3 5076
ps******@sympatico.ca (paul) wrote in message news:<b0**************************@posting.google. com>...
I have a query that takes monthly totals and rolls them up to get a
balance at a specific time code

eg:
dt bucket mon_ttl
--- ------ -------
199903 0192 -172527
199906 0192 546707
199909 0192 -278684
199910 0192 1100139
199911 0192 495279
199912 0192 -1690914
200003 0192 1025431
200006 0192 -1731678
200009 0192 1979256
200012 0192 -1273009

Rolls up like

DT BUCKET OPEN
--------- ---------- ----------
199903 0192 0
199906 0192 -172527
199909 0192 374180 (-172527+546707)
199910 0192 95496 etc...
199911 0192 1195635
199912 0192 1690914
200003 0192 0
200006 0192 1025431
200009 0192 -706247
200012 0192 1273009

I get that using

select dt, bucket, lag(run_ttl,1,0)
over (order by dt) open
from
(select dt, bucket, ttl,sum(ttl)
over (partition by bucket order by dt) run_ttl
from
mon_tot
where bucket='0192')

But I want to keep that last balance going forward...
eg: if I want the open as at 200301 and 200012 was the last non-null
balance
I want to carry that forward.
200012 0192 1273009
200101 0192 1273009
[..]
200301 0192 1273009

But I need to 'make up' the data going forward

I'm on 9.2.0.3 anyone have any tips/ideas?

You were almost there except you did not have the RANGE UNBOUNDED
PRECEDING clause. My suggestion is as follows:
SELECT
dt,
bucket,
SUM( ttl )
OVER(
PARTITION BY bucket
ORDER BY dt
RANGE UNBOUNDED PRECEDING
) run_ttl
FROM
mon_tot
WHERE
bucket='0192'
;

My test results are:
DT BUCK RUN_TTL
------ ---- ----------
199903 0192 -172527
199906 0192 374180
199909 0192 95496
199910 0192 1195635
199911 0192 1690914
199912 0192 0
200003 0192 1025431
200006 0192 -706247
200009 0192 1273009
200012 0192 0

10 rows selected.

I found this hint on p.560 of "Expert One-on-One Oracle" by Thomas
Kyte (A-Press:2003). He also has an on-line example at
http://asktom.oracle.com/pls/ask/f?p...D:305416600201

Douglas Hawthorne
Jul 19 '05 #2
thanks for the input
what you have shown me is the inner query I have before the LAG
-the default RANGE when not specified is UNBOUNDED PRECEDING
which in this case gives me ENDING balances that I lag(1,0) to get
opening balances. ie: 19903 open_bal=0, mon_ttl=-172527,
end_bal=-172527

What I want is to makeup data at the end of the range and keep the
ending balance going forward which is a little different than my
original request.
your recognition that i want to dump the lag and show the
ending_balance (and calculate the opening by taking off the current
activity has helped.
What I really want is this

DT BUCKET RUN_TTL
------ ---------- ----------
199903 0192 -172527
199906 0192 374180
199909 0192 95496
199910 0192 1195635
199911 0192 1690914
199912 0192 0
200003 0192 1025431
200006 0192 -706247
200009 0192 1273009
200012 0192 0
200101 0192 0
200102 0192 0
[..]
200301 0192 0

How can 'project' that last row forward?

I am trying in 10G now with partitioned outer joins, but that does not
seem to work if going off of analytic queries, only straight base
tables.

do**************@yahoo.com.au (Douglas Hawthorne) wrote in message news:<cf**************************@posting.google. com>...
ps******@sympatico.ca (paul) wrote in message news:<b0**************************@posting.google. com>...
I have a query that takes monthly totals and rolls them up to get a
balance at a specific time code

eg:
dt bucket mon_ttl
--- ------ -------
199903 0192 -172527
199906 0192 546707
199909 0192 -278684
199910 0192 1100139
199911 0192 495279
199912 0192 -1690914
200003 0192 1025431
200006 0192 -1731678
200009 0192 1979256
200012 0192 -1273009

Rolls up like

DT BUCKET OPEN
--------- ---------- ----------
199903 0192 0
199906 0192 -172527
199909 0192 374180 (-172527+546707)
199910 0192 95496 etc...
199911 0192 1195635
199912 0192 1690914
200003 0192 0
200006 0192 1025431
200009 0192 -706247
200012 0192 1273009

I get that using

select dt, bucket, lag(run_ttl,1,0)
over (order by dt) open
from
(select dt, bucket, ttl,sum(ttl)
over (partition by bucket order by dt) run_ttl
from
mon_tot
where bucket='0192')

But I want to keep that last balance going forward...
eg: if I want the open as at 200301 and 200012 was the last non-null
balance
I want to carry that forward.
200012 0192 1273009
200101 0192 1273009
[..]
200301 0192 1273009

But I need to 'make up' the data going forward

I'm on 9.2.0.3 anyone have any tips/ideas?

You were almost there except you did not have the RANGE UNBOUNDED
PRECEDING clause. My suggestion is as follows:
SELECT
dt,
bucket,
SUM( ttl )
OVER(
PARTITION BY bucket
ORDER BY dt
RANGE UNBOUNDED PRECEDING
) run_ttl
FROM
mon_tot
WHERE
bucket='0192'
;

My test results are:
DT BUCK RUN_TTL
------ ---- ----------
199903 0192 -172527
199906 0192 374180
199909 0192 95496
199910 0192 1195635
199911 0192 1690914
199912 0192 0
200003 0192 1025431
200006 0192 -706247
200009 0192 1273009
200012 0192 0

10 rows selected.

I found this hint on p.560 of "Expert One-on-One Oracle" by Thomas
Kyte (A-Press:2003). He also has an on-line example at
http://asktom.oracle.com/pls/ask/f?p...D:305416600201

Douglas Hawthorne

Jul 19 '05 #3
"paul" <ps******@sympatico.ca> wrote in message
news:b0**************************@posting.google.c om...
thanks for the input
what you have shown me is the inner query I have before the LAG
-the default RANGE when not specified is UNBOUNDED PRECEDING
which in this case gives me ENDING balances that I lag(1,0) to get
opening balances. ie: 19903 open_bal=0, mon_ttl=-172527,
end_bal=-172527

What I want is to makeup data at the end of the range and keep the
ending balance going forward which is a little different than my
original request.
your recognition that i want to dump the lag and show the
ending_balance (and calculate the opening by taking off the current
activity has helped.
What I really want is this

DT BUCKET RUN_TTL
------ ---------- ----------
199903 0192 -172527
199906 0192 374180
199909 0192 95496
199910 0192 1195635
199911 0192 1690914
199912 0192 0
200003 0192 1025431
200006 0192 -706247
200009 0192 1273009
200012 0192 0
200101 0192 0
200102 0192 0
[..]
200301 0192 0

How can 'project' that last row forward?

I am trying in 10G now with partitioned outer joins, but that does not
seem to work if going off of analytic queries, only straight base
tables.

do**************@yahoo.com.au (Douglas Hawthorne) wrote in message

news:<cf**************************@posting.google. com>...
ps******@sympatico.ca (paul) wrote in message news:<b0**************************@posting.google. com>...
I have a query that takes monthly totals and rolls them up to get a
balance at a specific time code

eg:
dt bucket mon_ttl
--- ------ -------
199903 0192 -172527
199906 0192 546707
199909 0192 -278684
199910 0192 1100139
199911 0192 495279
199912 0192 -1690914
200003 0192 1025431
200006 0192 -1731678
200009 0192 1979256
200012 0192 -1273009

Rolls up like

DT BUCKET OPEN
--------- ---------- ----------
199903 0192 0
199906 0192 -172527
199909 0192 374180 (-172527+546707)
199910 0192 95496 etc...
199911 0192 1195635
199912 0192 1690914
200003 0192 0
200006 0192 1025431
200009 0192 -706247
200012 0192 1273009

I get that using

select dt, bucket, lag(run_ttl,1,0)
over (order by dt) open
from
(select dt, bucket, ttl,sum(ttl)
over (partition by bucket order by dt) run_ttl
from
mon_tot
where bucket='0192')

But I want to keep that last balance going forward...
eg: if I want the open as at 200301 and 200012 was the last non-null
balance
I want to carry that forward.
200012 0192 1273009
200101 0192 1273009
[..]
200301 0192 1273009

But I need to 'make up' the data going forward

I'm on 9.2.0.3 anyone have any tips/ideas?

You were almost there except you did not have the RANGE UNBOUNDED
PRECEDING clause. My suggestion is as follows:
SELECT
dt,
bucket,
SUM( ttl )
OVER(
PARTITION BY bucket
ORDER BY dt
RANGE UNBOUNDED PRECEDING
) run_ttl
FROM
mon_tot
WHERE
bucket='0192'
;

My test results are:
DT BUCK RUN_TTL
------ ---- ----------
199903 0192 -172527
199906 0192 374180
199909 0192 95496
199910 0192 1195635
199911 0192 1690914
199912 0192 0
200003 0192 1025431
200006 0192 -706247
200009 0192 1273009
200012 0192 0

10 rows selected.

I found this hint on p.560 of "Expert One-on-One Oracle" by Thomas
Kyte (A-Press:2003). He also has an on-line example at
http://asktom.oracle.com/pls/ask/f?p...D:305416600201
Douglas Hawthorne


Paul,

What you want is a source of rows that have the generated DT and BUCKET
columns with NULL for the TTL column. My suggested solution is to use a
'temporary' table to store the projected values of DT for every BUCKET
value. I then modified my original query to be over an in-line view that is
a UNION ALL of the original table and the projected values table.

First, I created the 'temporary' table to hold the projected values using
CTAS and selecting no rows:
CREATE TABLE projected_mon_tot
AS SELECT * FROM mon_tot
WHERE 1=0
;

Second, I populated this table based on the current values from the MON_TOT
table:
INSERT INTO projected_mon_tot
SELECT
TO_CHAR(
ADD_MONTHS(
TO_DATE(
t.base_dt,
'YYYYMM'
),
a.num_months
),
'YYYYMM'
) AS dt,
t.bucket,
NULL AS ttl
FROM
(
SELECT
rownum AS num_months
FROM
all_objects
WHERE
rownum <= 25
) a,
(
SELECT
MAX( dt ) AS base_dt,
bucket
FROM
mon_tot
GROUP BY
bucket
) t
;

Here I am projecting 25 months past the last DT value for each value in the
BUCKET column by using a cartesian join between a range of row numbers from
the ALL_OBJECTS table and the calculated maximum value of DT for each value
of BUCKET. I have assumed that the DT column is of data type, VARCHAR2 or
CHAR.

Third, I ran the modified query:
SELECT
dt,
bucket,
SUM( ttl )
OVER(
PARTITION BY bucket
ORDER BY dt
) run_ttl
FROM
(
SELECT
*
FROM
mon_tot
UNION ALL
SELECT
*
FROM
projected_mon_tot
)
WHERE
bucket='0192'
;

The only difference between this query and my original one is the use of the
in-line view with the UNION ALL. I used UNION ALL because I know that there
are no overlaps in values between the two tables.

Thanks for pointing out that RANGE UNBOUNDED PRECEEDING is the default.

The result is:

DT BUCK RUN_TTL
------ ---- ----------
199903 0192 -172527
199906 0192 374180
199909 0192 95496
199910 0192 1195635
199911 0192 1690914
199912 0192 0
200003 0192 1025431
200006 0192 -706247
200009 0192 1273009
200012 0192 0
200101 0192 0
200102 0192 0
200103 0192 0
200104 0192 0
200105 0192 0
200106 0192 0
200107 0192 0
200108 0192 0
200109 0192 0
200110 0192 0
200111 0192 0
200112 0192 0
200201 0192 0
200202 0192 0
200203 0192 0
200204 0192 0
200205 0192 0
200206 0192 0
200207 0192 0
200208 0192 0
200209 0192 0
200210 0192 0
200211 0192 0
200212 0192 0
200301 0192 0

35 rows selected.

Douglas Hawthorne
Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jayjay | last post by:
When it comes to access, I'm pretty good using the built in features and can come up with some pretty complex functions to get what I need. But we have this database I'm doing for work that is...
3
by: dd_bdlm | last post by:
Please help this one is driving me mad! I have searched and read all the topics on the error message I am receiving but none seem to apply to me! I have quite a complex query linking all parts...
6
by: KashMarsh | last post by:
Trying to show running totals on a report, except it needs to show one total amount and values being subtracted from it. For example, the report shows a Total Inventory amount (TotInvAmt). And...
3
by: Marina | last post by:
I have a querie that needs to use multiple calculations (...I think...) Fields: TransID TransDate TransDescipt ClientID HoursBilled BillingRate
7
by: Siv | last post by:
Hi, I have a stored procedure that I want to execute and then wait in a loop showing a timer whilst it completes and then carry on once I get notification that it has completed. The main reason...
2
by: Jana | last post by:
Using Access 97. Background: I have a main report called rptTrustHeader with a subreport rptTrustDetails in the Details section of the main report. The main report is grouped by MasterClientID. ...
3
by: =?Utf-8?B?Um9iZXJ0IENoYXBtYW4=?= | last post by:
Hi, Fairly easy to create one running total for a gridview but what if you have dozens of them? I have a gridview that allows bulk editing (all rows at once) and have it set up so that, on data...
3
by: mochatrpl | last post by:
I am looking for a way to make a query / report display the running average for total dollars. I have already set up a query to provide totals dollars per day from which a report graphly shows...
6
by: Stuart Shay | last post by:
Hello All: I have a array which contains the totals for each month and from this array I want to get a running total for each month decimal month = new decimal; month = 254; (Jan) month =...
4
by: mattehz | last post by:
Hey there, I am trying to upload old source files and came across these errors: Warning: Invalid argument supplied for foreach() in /home/mattehz/public_html/acssr/trunk/inc_html.php on line 59...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.