Solved

Oracle sum question

Posted on 2012-03-26
4
491 Views
Last Modified: 2012-06-21
Having table TestTB (dates in dd-mm-yyyy)

Item;Date;qty
A;12-01-2012;5
A;27-01-2012;-2
A;04-02-2012;1
B;08-11-2011;-1
B;08-11-2011;3
B;15-11-2011;7
C;17-01-2012;-5
C;25-02-2012;3

I need A Select Sum pr. month-Year - summarized (Balance)

Item;Mont-;Balance
A;01-2012;3
A;02-2012;4         (that is 3 from 01 and 1 from 02)
B;11-2011;9
C;01-2012;-5
C;02-2012;-2       (that is -5 from 01 and 3 from 02)

I use Oracle 10g
0
Comment
Question by:Vinum
4 Comments
 
LVL 76

Expert Comment

by:slightwv (䄆 Netminder)
ID: 37765495
I don't see how you get itemA numbers but based on what I think you want, something like:  

select item, to_char(date_col,'MM-YYYY'), sum(qty) from table
group by item, to_char(date_col,'MM-YYYY');
0
 

Author Comment

by:Vinum
ID: 37765575
Hi

It is not just a sum pr. month - it is a balance.

Example with item A:
The sum/balance for 01-2012 is 3 (5 and -2)
The sum/balance for 02-2012 is 4 (1 for month 02 and 3 for months 01)


The idea is to make a balance over time.
Another example:
I have 5 of X on stock.
In January I sell 2, balance in the end of January is 3 (5-2)
In February I sell 1 and buy 10, balance at the end of February is 12 (5-2-1+10)
In March I sell 5, balance at the end of March i 7 (5-2-1+10-5)
and so on....

X;01-2012;3
X;02-2012;12
X;03-2012;7
.
.
0
 
LVL 35

Expert Comment

by:Robert Schutt
ID: 37765937
I think you can only do that with a subselect:

select distinct tb1.item, 
   to_char(tb1.dat, 'mm-yyyy') per, 
   (select sum(qty) 
    from testtb tb2 
    where tb2.item = tb1.item and 
      to_char(tb2.dat, 'yyyymm') <= to_char(tb1.dat, 'yyyymm')) balance 
from testtb tb1 
group by tb1.item, tb1.dat 
order by tb1.item, per;

Open in new window

0
 
LVL 27

Accepted Solution

by:
sujith80 earned 500 total points
ID: 37766297
See the below, combine a group by with analytical function -
SQL> select *
  2  from tbl1;

ITEM                 DT                 QTY
-------------------- ----------- ----------
A                    12-jan-2012          5
A                    27-jan-2012         -2
A                    04-feb-2012          1
B                    08-nov-2011         -1
B                    08-nov-2011          3
B                    15-nov-2011          7
C                    17-jan-2012         -5
C                    25-feb-2012          3

8 rows selected.

SQL>
SQL> select item, dt month, sum(qty) over( partition by item order by to_date(dt, 'mm-yyyy')) balance
  2  from (
  3  select item, to_char(dt, 'mm-yyyy') dt, sum(qty) qty
  4  from tbl1
  5  group by item, to_char(dt, 'mm-yyyy')
  6  )
  7  order by 1,2;

ITEM                 MONTH      BALANCE
-------------------- ------- ----------
A                    01-2012          3
A                    02-2012          4
B                    11-2011          9
C                    01-2012         -5
C                    02-2012         -2

SQL>

Open in new window

0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Oracle sql query 7 74
pl/sql - query very slow 26 71
minium over 4 numeric columns for each row in oracle 2 28
Oracle cursor lifecycle inside procedure. 2 11
Configuring and using Oracle Database Gateway for ODBC Introduction First, a brief summary of what a Database Gateway is.  A Gateway is a set of driver agents and configurations that allow an Oracle database to communicate with other platforms…
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
This video explains at a high level with the mandatory Oracle Memory processes are as well as touching on some of the more common optional ones.
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question