Home » SQL & PL/SQL » SQL & PL/SQL » Analytic Report query (Oracle 11g)
Analytic Report query [message #652998] |
Thu, 23 June 2016 07:38 |
srinivas.k2005
Messages: 404 Registered: August 2006
|
Senior Member |
|
|
Hi,
Create table x_test (
bill_num varchar2(50),
inv_num varchar2(50),
inv_date date,
amount number
);
Insert into x_test values ('101','5001',to_date('09-02-2012','mm-dd-yyyy'),30);
Insert into x_test values ('101','5002',to_date('09-02-2013','mm-dd-yyyy'),20);
Insert into x_test values ('101','5003',to_date('09-02-2014','mm-dd-yyyy'),60);
Insert into x_test values ('101','5004',to_date('09-02-2015','mm-dd-yyyy'),100);
Insert into x_test values ('201','6001',to_date('07-02-2012','mm-dd-yyyy'),200);
Insert into x_test values ('201','6002',to_date('07-02-2013','mm-dd-yyyy'),40);
Insert into x_test values ('201','6003',to_date('07-02-2014','mm-dd-yyyy'),90);
Insert into x_test values ('201','6004',to_date('07-02-2015','mm-dd-yyyy'),80);
SQL> select * from x_test order by bill_num,inv_date desc;
BILL_NUM INV_NUM INV_DATE AMOUNT
-------------------------------------------------- -------------------------------------------------- ----------- ----------
101 5004 9/2/2015 100
101 5003 9/2/2014 60
101 5002 9/2/2013 20
101 5001 9/2/2012 30
201 6004 7/2/2015 80
201 6003 7/2/2014 90
201 6002 7/2/2013 40
201 6001 7/2/2012 200
SQL> select * from x_test where bill_num=101 order by inv_date desc;
BILL_NUM INV_NUM INV_DATE AMOUNT
-------------------------------------------------- -------------------------------------------------- ----------- ----------
101 5004 9/2/2015 100
101 5003 9/2/2014 60
101 5002 9/2/2013 20
101 5001 9/2/2012 30
Expected Result:
Taking example for bill_num 101,
BILL_NUM INV_NUM INV_DATE SUM_AMOUNT
-------------------------------------------------- -------------------------------------------------- ----------- ----------
101 5004 9/2/2015 110
101 5003 9/2/2014 50
101 5002 9/2/2013 30
101 5001 9/2/2012
Sum_amount for INV_NUM 5004 is calculated by summing up all amount based on INV_DATE which is less than INV_DATE of 5004
for that particualt bill_num.
So here, for INV_NUM 5004, sum_amount is calcluated as (60+20+30 = 110).
For INV_NUM 5003, sum_amount is calcluated as (20+30 = 50).
For last INV_NUM 5001, there is no inv_date which is less than the current one and sum_amount should be shown as null.
Please advise.
Regards,
SRK
|
|
|
Re: Analytic Report query [message #653006 is a reply to message #652998] |
Thu, 23 June 2016 08:13 |
Bill B
Messages: 1971 Registered: December 2004
|
Senior Member |
|
|
SELECT Bill_num,
Inv_num,
Inv_date,
SUM (
Amount)
OVER (PARTITION BY Bill_num
ORDER BY Inv_date
ROWS BETWEEN UNBOUNDED PRECEDING AND 1 PRECEDING)
SUM
FROM X_test
WHERE Bill_num = '101'
ORDER BY Inv_date DESC;
BILL_NUM INV_NUM INV_DATE SUM
---------- -------------- --------- ----------
101 5004 02-SEP-15 110
101 5003 02-SEP-14 50
101 5002 02-SEP-13 30
101 5001 02-SEP-12
[Updated on: Thu, 23 June 2016 08:14] Report message to a moderator
|
|
|
|
Goto Forum:
Current Time: Fri Apr 26 06:47:41 CDT 2024
|