I need some help with some SQL pleeeaasseee....

I'm trying to do some summay work with some data using some of the 
Oracle9i Analytic functions.

Here is the sample data:


SQL> select * from sum_test;

        ID TODAY     CODE                      VALUE
---------- --------- -------------------- ----------
         1 02-FEB-03 A                           100
         1 02-FEB-03 A                           200
         1 02-FEB-03 B                            50
         1 03-FEB-03 A                            50
         1 03-FEB-03 B                            25
         1 05-FEB-03 A                           725
         1 05-FEB-03 C                           125
         1 02-FEB-03 A                           100
         1 03-FEB-03 A                           100
         2 02-FEB-03 A                           100
         2 03-FEB-03 A                           100
         2 04-FEB-03 B                           100

and here is how I want the output to look:
Note that I want:
1. for the first day I want total by ID, tday (truncated date) and code. I
want the daily total in the VAL_TOTAL column
2. For each subsiquent day, I want that days total in val_total ADDED
to the previous total.

        ID TDAY      CODE                      VALUE  VAL_TOTAL
---------- --------- -------------------- ---------- ----------
         1 02-FEB-03 A                           100        400 
         1 02-FEB-03 A                           200        400
         1 02-FEB-03 A                           100        400
         1 02-FEB-03 B                            50         50
         2 02-FEB-03 A                           100        100
         1 03-FEB-03 A                            50        550
         1 03-FEB-03 A                           100        550
         1 03-FEB-03 B                            25         75
         2 03-FEB-03 A                           100        750
         2 04-FEB-03 B                           100        175
         1 05-FEB-03 A                           725       1275
         1 05-FEB-03 C                           125        125

Here is the query I'm trying:

select b.id, trunc(b.today) tday, b.code, b.value, 
sum(a.val_total) over
   (partition by  b.id, trunc(b.today),  b.code order by b.id,
trunc(b.today)
   range between unbounded preceding and unbounded following) val_total
from
(
   select id, trunc(today) today, code, sum(value) val_total
   from sum_test
   group by id, trunc(today), code
) a,
sum_test b
   where a.id=b.id
   and trunc(b.today)=a.today
   and a.code=b.code
   order by 2, 1
/

Results...

        ID TDAY      CODE                      VALUE  VAL_TOTAL
---------- --------- -------------------- ---------- ----------
         1 02-FEB-03 A                           100       1200
         1 02-FEB-03 A                           200       1200
         1 02-FEB-03 A                           100       1200
         1 02-FEB-03 B                            50         50
         2 02-FEB-03 A                           100        100
         1 03-FEB-03 A                            50        300
         1 03-FEB-03 A                           100        300
         1 03-FEB-03 B                            25         25
         2 03-FEB-03 A                           100        100
         2 04-FEB-03 B                           100        100
         1 05-FEB-03 A                           725        725
         1 05-FEB-03 C                           125        125
Obviously not correct. I know I can do this with a self join to the table,
but I was hopeful that I could do it this way. Any ideas?         





-- 
Please see the official ORACLE-L FAQ: http://www.orafaq.net
-- 
Author: Freeman Robert - IL
  INET: [EMAIL PROTECTED]

Fat City Network Services    -- 858-538-5051 http://www.fatcity.com
San Diego, California        -- Mailing list and web hosting services
---------------------------------------------------------------------
To REMOVE yourself from this mailing list, send an E-Mail message
to: [EMAIL PROTECTED] (note EXACT spelling of 'ListGuru') and in
the message BODY, include a line containing: UNSUB ORACLE-L
(or the name of mailing list you want to be removed from).  You may
also send the HELP command for other information (like subscribing).

Reply via email to