Age analysis on excel - oracle

I would like to distribute a set of data into a table shown below:
RANGE(days) No of days. Amount
0-30 - 0
31-180 1 4,185.78
181-365 2 74,056.86
366 and above 6 587,198.35
TOTAL 9 665,440.99
SOURCE Data
S/N START DATE Details Tran Amt END DATE
1 22/05/2015 A 448,749.14 30/06/2018
2 22/09/2015 B 4,883.02 30/06/2018
3 04/11/2015 C 45,646.27 30/06/2018
4 26/04/2016 D 42,861.99 30/06/2018
5 16/06/2016 E 23,144.23 30/06/2018
6 27/07/2016 F 21,913.70 30/06/2018
7 11/08/2017 G 61,396.94 30/06/2018
8 30/11/2017 H 12,659.92 30/06/2018
9 19/03/2018 I 4,185.78 30/06/2018
TOTAL 665,440.99
Thanks

SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE data ( SN, START_DATE, Details, TranAmt ) AS
SELECT 1, DATE '2015-05-22', 'A', 448749.14 FROM DUAL UNION ALL
SELECT 2, DATE '2015-09-22', 'B', 4883.02 FROM DUAL UNION ALL
SELECT 3, DATE '2015-11-04', 'C', 45646.27 FROM DUAL UNION ALL
SELECT 4, DATE '2016-04-26', 'D', 42861.99 FROM DUAL UNION ALL
SELECT 5, DATE '2016-06-16', 'E', 23144.23 FROM DUAL UNION ALL
SELECT 6, DATE '2016-07-27', 'F', 21913.70 FROM DUAL UNION ALL
SELECT 7, DATE '2017-08-11', 'G', 61396.94 FROM DUAL UNION ALL
SELECT 8, DATE '2017-11-30', 'H', 12659.92 FROM DUAL UNION ALL
SELECT 9, DATE '2018-03-19', 'I', 4185.78 FROM DUAL;
Query 1:
WITH end_date ( end_date ) AS (
SELECT DATE '2018-06-30' FROM DUAL
),
ranges ( first, last ) AS (
SELECT 0, 30 FROM DUAL UNION ALL
SELECT 31, 180 FROM DUAL UNION ALL
SELECT 181, 365 FROM DUAL UNION ALL
SELECT 366, NULL FROM DUAL
)
SELECT NVL2(
r.first,
r.first || NVL2(
MAX( r.last ),
' - ' || MAX( r.last ),
' and above'
),
'Total'
) AS range,
COUNT( d.sn ) AS "No of days",
COALESCE( SUM( d.TranAmt ), 0 ) AS Amount
FROM data d
CROSS JOIN end_date e
RIGHT OUTER JOIN ranges r
ON ( d.start_date < e.end_date + 1 - r.first
AND ( r.last IS NULL
OR d.start_date >= e.end_date - r.last ) )
GROUP BY ROLLUP( r.first )
ORDER BY r.first
Results:
| RANGE | No of days | AMOUNT |
|---------------|------------|-----------|
| 0 - 30 | 0 | 0 |
| 31 - 180 | 1 | 4185.78 |
| 181 - 365 | 2 | 74056.86 |
| 366 and above | 6 | 587198.35 |
| Total | 9 | 665440.99 |

Related

Oracle SQL multi-level pivot groups

EDIT: To those saying this is a clear & obvious "No": Sure, I figured that was the case, and hierarchical headers were beyond the scope of SQL query results. However, apart from some Mysql work, I've just made the jump from an old legacy SQL Server 2000 platform to Oracle 12g, and finding things there that I could never have imagined doing in SS 2000, so I thought I'd ask. I write loads of SQL to feed my presentation layer in a few report creation systems, and so I'm exploring this leap forward in capabilities from SS 2000.
I may be asking too much of the Oracle Pivot function, but this is what I'm trying to do. I can pivot at a single level but I want a hierarchy of column grouping with multiple measures the way you could easily do in a spreadsheet crosstab. Here's sample data & desired output:
select *
from(
select 'A' rws, 'X' cols, 2 v1, 90 v2 from dual union
select 'A' rws, 'Y' cols, 25 v1, 112 v2 from dual union
select 'A' rws, 'Y' cols, 7 v1, 64 v2 from dual union
select 'B' rws, 'X' cols, 4 v1, 117 v2 from dual union
select 'B' rws, 'Y' cols, 46 v1, 32 v2 from dual union
select 'B' rws, 'X' cols, 0 v1, 18 v2 from dual
)
Here is the output I would like:
-----------------------------------------------------------
| A | B |
-----------------------------------------------------------
| X | Y | X | Y |
-----------------------------------------------------------
| v1 | v2 | v1 | v2 | v1 | v2 | v1 | v2 |
-----------------------------------------------------------
| 2 | 90 | 32 | 176 | 4 | 135 | 46 | 32 |
-----------------------------------------------------------
Of course, you can pivot data as you want, but you need to format header yourself, since onviously Oracle return standard table data:
select *
from(
select 'A' rws, 'X' cols, 2 v1, 90 v2 from dual union
select 'A' rws, 'Y' cols, 25 v1, 112 v2 from dual union
select 'A' rws, 'Y' cols, 7 v1, 64 v2 from dual union
select 'B' rws, 'X' cols, 4 v1, 117 v2 from dual union
select 'B' rws, 'Y' cols, 46 v1, 32 v2 from dual union
select 'B' rws, 'X' cols, 0 v1, 18 v2 from dual
) t
pivot
(
max(v1) as v1_,max(v2) as v2_
for (rws,cols) in (
('A','X') as A_X,
('A','Y') as A_Y,
('B','X') as B_X,
('B','Y') as B_Y
)
);
Result:
A_X_V1_ A_X_V2_ A_Y_V1_ A_Y_V2_ B_X_V1_ B_X_V2_ B_Y_V1_ B_Y_V2_
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
2 90 25 112 4 117 46 32
You can use the conditional aggregation as follows:
SQL> SELECT SUM(CASE WHEN RWS = 'A' AND COLS = 'X' THEN V1 END) AS AXV1,
2 SUM(CASE WHEN RWS = 'A' AND COLS = 'X' THEN V2 END) AS AXV2,
3 SUM(CASE WHEN RWS = 'A' AND COLS = 'Y' THEN V1 END) AS AYV1,
4 SUM(CASE WHEN RWS = 'A' AND COLS = 'Y' THEN V2 END) AS AYV2,
5 SUM(CASE WHEN RWS = 'B' AND COLS = 'X' THEN V1 END) AS BXV1,
6 SUM(CASE WHEN RWS = 'B' AND COLS = 'X' THEN V2 END) AS BXV2,
7 SUM(CASE WHEN RWS = 'B' AND COLS = 'Y' THEN V1 END) AS BYV1,
8 SUM(CASE WHEN RWS = 'B' AND COLS = 'Y' THEN V2 END) AS BYV2
9 FROM YOUR_TABLE;
AXV1 AXV2 AYV1 AYV2 BXV1 BXV2 BYV1 BYV2
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
2 90 32 176 4 135 46 32
SQL>
Showing the multi-line headers must be taken care from application side.

multiple columns' row split in oracle

I got a single string row split into rows
For example,
A,B,C,D,E
into
A
B
C
D
E
but what I would like to is multiple columns' single row
| A,B,C | H,I,J,K,L | Q,R,X,Y,Z |
into
A | H | Q
B | I | R
C | J | X
| K | Y
| L |
How can I do this in oracle?
You can use hiearchy query as follows:
SQL> WITH DATAA ( D ) AS (
2 SELECT '| A,B,C | H,I,J,K,L | Q,R,X,Y,Z |'
3 FROM DUAL
4 )
5 -- your query starts from here
6 SELECT TRIM(REGEXP_SUBSTR(REGEXP_SUBSTR(D.D, '[^|]+', 1, 1), '[^,]+', 1, LEVEL)) AS COL1,
7 TRIM(REGEXP_SUBSTR(REGEXP_SUBSTR(D.D, '[^|]+', 1, 2), '[^,]+', 1, LEVEL)) AS COL2,
8 TRIM(REGEXP_SUBSTR(REGEXP_SUBSTR(D.D, '[^|]+', 1, 3), '[^,]+', 1, LEVEL)) AS COL3
9 FROM DATAA D
10 CONNECT BY LEVEL <= (
11 SELECT MAX(REGEXP_COUNT((REGEXP_SUBSTR(D.D, '[^|]+', 1, COLUMN_VALUE)), ',')) + 1
12 FROM DATAA D
13 CROSS JOIN TABLE ( CAST(MULTISET(
14 SELECT LEVEL LVL
15 FROM DUAL
16 CONNECT BY LEVEL <= REGEXP_COUNT(D.D, '[^|]+')
17 ) AS SYS.ODCIVARCHAR2LIST) ) LVLS
18 );
COL1 COL2 COL3
------- ----------- -----------
A H Q
B I R
C J X
K Y
L Z
SQL>

Oracle find the 1st node and last node in a path, CONNECT BY nocycle

I would like to know if there is anyway it return the beginning of the hierarchy and end of hierarchy for the code below
WITH o AS
(
SELECT 'A' as obj,
'C' as link
FROM dual
UNION ALL
SELECT 'C',
'D'
FROM dual
UNION ALL
SELECT 'D',
'E'
FROM dual
UNION ALL
SELECT 'X',
'Y'
FROM dual),
apath AS
(SELECT obj,
link
, sys_connect_by_path(obj || '->' || link, ',') pth
, connect_by_iscycle as cy
, connect_by_isleaf AS lf
, level
FROM o
CONNECT BY nocycle obj = PRIOR link)
SELECT *
FROM apath
where lf = 1
order by pth
I would like have the result like
Begin_Node, End_Node
A E
C E
D E
X Y
Current code returns
D E ,A->C,C->D,D->E 0 1 3
D E ,C->D,D->E 0 1 2
D E ,D->E 0 1 1
X Y ,X->Y 0 1 1
Thanks in advance.
Just use CONNECT_BY_ROOT and filter by CONNECT_BY_ISLEAF:
WITH o ( obj, link ) AS (
SELECT 'A', 'C' FROM DUAL UNION ALL
SELECT 'C', 'D' FROM DUAL UNION ALL
SELECT 'D', 'E' FROM DUAL UNION ALL
SELECT 'X', 'Y' FROM DUAL
)
SELECT CONNECT_BY_ROOT( obj ) AS begin_node,
link AS end_node
FROM o
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY nocycle obj = PRIOR link;
Outputs:
BEGIN_NODE | END_NODE
:--------- | :-------
A | E
C | E
D | E
X | Y
If you want all the columns then:
WITH o ( obj, link ) AS (
SELECT 'A', 'C' FROM DUAL UNION ALL
SELECT 'C', 'D' FROM dual UNION ALL
SELECT 'D', 'E' FROM dual UNION ALL
SELECT 'X', 'Y' FROM dual
)
SELECT CONNECT_BY_ROOT( obj ) AS begin_node,
link AS end_node,
SYS_CONNECT_BY_PATH( obj || '->' || link, ',' ) AS path,
CONNECT_BY_ISCYCLE as iscycle,
CONNECT_BY_ISLEAF AS isleaf,
level
FROM o
WHERE CONNECT_BY_ISLEAF = 1
CONNECT BY nocycle obj = PRIOR link;
Which outputs:
BEGIN_NODE | END_NODE | PATH | ISCYCLE | ISLEAF | LEVEL
:--------- | :------- | :-------------- | ------: | -----: | ----:
A | E | ,A->C,C->D,D->E | 0 | 1 | 3
C | E | ,C->D,D->E | 0 | 1 | 2
D | E | ,D->E | 0 | 1 | 1
X | Y | ,X->Y | 0 | 1 | 1
db<>fiddle here
If you're happy with what pth represents, it is simple to extract that info from it:
line #21: remove leading comma
lines #27 and #28 return begin and end nodes
<snip>
18 apath AS
19 (SELECT obj,
20 link
21 , ltrim(sys_connect_by_path(obj || '->' || link, ','), ',') pth
22 , connect_by_iscycle as cy
23 , connect_by_isleaf AS lf
24 , level
25 FROM o
26 CONNECT BY nocycle obj = PRIOR link)
27 SELECT regexp_substr(pth, '^\w+') begin_node,
28 regexp_substr(pth, '\w+$') end_node
29 FROM apath
30 where lf = 1
31 order by pth
32 /
BEGIN_NODE END_NODE
---------- ----------
A E
C E
D E
X Y
SQL>

Sum/divide values from View results

I have 4 Views with columns and results like this:
___________________________________
| Account_no | Region_code | Total |
|-----------------------------------
|123456789 | 123 | 321,34 |
|212234567 | 543 | 214 |
|076948329 | 100 | 310 |
|093290432 | 320 | 1200,44|
|346574554 | 123 | 542,01 |
|___________________________________|
All these views has calculated column Total, but in order to get a complete result for each account in region I need to calculate Total again, with a formula like this : View1.Total - View2.Total + View3.Total - View4.Total.
Can somebody show me an example on how to achieve this in Oracle ?
Something like this (simplified)?
SQL> with
2 view1 (accno, total) as
3 (select 1, 100 from dual union
4 select 2, 200 from dual
5 ),
6 view2 (accno, total) as
7 (select 1, 10 from dual union
8 select 2, 30 from dual
9 ),
10 view3 (accno, total) as
11 (select 1, 20 from dual union
12 select 2, 40 from dual
13 ),
14 view4 (accno, total) as
15 (select 1, 40 from dual union
16 select 2, 30 from dual
17 )
18 --
19 select v1.accno,
20 (v1.total - v2.total + v3.total - v4.total) total_total
21 from view1 v1 join view2 v2 on v1.accno = v2.accno
22 join view3 v3 on v1.accno = v3.accno
23 join view4 v4 on v1.accno = v4.accno;
ACCNO TOTAL_TOTAL
---------- -----------
1 70
2 180
SQL>

Oracle Self Join to retrieve recursive data

I have the following schema and data
segmentid paramid paramvalue
103 1 4418
101 1 4834
102 1 5587
104 1 7413
105 1 9965
106 1 7421
107 1 7782
103 2 1990|2000
102 2 2005|2010
105 2 1985|1990
104 2 1981
101 3 F
103 3 M
101 4 M
103 4 S
102 5 SUKKHUR
105 5 LAHORE
106 5 HYDRABAD
107 5 KHAIRPUR
101 5 ISLAMABAD
Now i will have inputs of different param values like Karachi M and date of birth range. I want to retrieve only that segment id whose all parameters are returned to be true.
If any parameter is failed the segment should fail.
Below is my idea but its returning if any paramvalue is true as ive used or but when i and the data is not retrieved.
select tpv.* from tblsegment ts , tblsegmentparameter tsp , tblsegmentparamvalue tpv
where ts.segmentid = tpv.segmentid and tsp.parameterid = tpv.paramid
and
(
(lower(tsp.paramname) like 'city' and tpv.paramvalue = 'KARACHI' and tsp.parameterid = tpv.paramid)
or
(lower(tsp.paramname) like 'gender' and tpv.paramvalue = 'M')
or
(lower(tsp.paramname) like 'maritalstatus' and tpv.paramvalue = 'S')
or
(lower(tsp.paramname) like 'product' and tpv.paramvalue = (select distinct ta.productid from tblcustchannelacct ta ,tblcustomer tc, tblaccount tta
where ta.relationship_id = '5327016301000015=5311' and ta.channel_id = '0001' and ta.account_id = tta.account_id and ta.customer_id = tc.customerid )
)
or
(lower(tsp.paramname) like 'dob' and
(
(
to_char( '1985') between
to_char( REGEXP_SUBSTR ( tpv.paramvalue, '^[^|]*'))
and
to_char(REGEXP_SUBSTR( tpv.paramvalue, '*[^|]*$'))
) or
(
to_char( '1986') = tpv.paramvalue
)
)
)
)
order by tsp.sortorder;
SQL Fiddle
Oracle 11g R2 Schema Setup:
CREATE TABLE tblsegmentparamvalue ( segmentid, paramid, paramvalue ) AS
SELECT 103, 1, '4418' FROM DUAL
UNION ALL SELECT 101, 1, '4834' FROM DUAL
UNION ALL SELECT 102, 1, '5587' FROM DUAL
UNION ALL SELECT 104, 1, '7413' FROM DUAL
UNION ALL SELECT 105, 1, '9965' FROM DUAL
UNION ALL SELECT 106, 1, '7421' FROM DUAL
UNION ALL SELECT 107, 1, '7782' FROM DUAL
UNION ALL SELECT 103, 2, '1990|2000' FROM DUAL
UNION ALL SELECT 102, 2, '2005|2010' FROM DUAL
UNION ALL SELECT 105, 2, '1985|1990' FROM DUAL
UNION ALL SELECT 104, 2, '1981' FROM DUAL
UNION ALL SELECT 101, 3, 'F' FROM DUAL
UNION ALL SELECT 103, 3, 'M' FROM DUAL
UNION ALL SELECT 101, 4, 'M' FROM DUAL
UNION ALL SELECT 103, 4, 'S' FROM DUAL
UNION ALL SELECT 102, 5, 'SUKKHUR' FROM DUAL
UNION ALL SELECT 105, 5, 'LAHORE' FROM DUAL
UNION ALL SELECT 106, 5, 'HYDRABAD' FROM DUAL
UNION ALL SELECT 107, 5, 'KHAIRPUR' FROM DUAL
UNION ALL SELECT 101, 5, 'ISLAMABAD' FROM DUAL;
Query 1:
WITH segments AS (
SELECT segmentid
FROM tblsegmentparamvalue
GROUP BY segmentid
HAVING ( COUNT( CASE WHEN paramid = 1 THEN 1 END ) = 0
OR COUNT( CASE WHEN paramid = 1 AND paramvalue = '4834' THEN 1 END ) > 0 )
AND ( COUNT( CASE WHEN paramid = 2 THEN 1 END ) = 0
OR COUNT( CASE WHEN paramid = 2 AND '1985' BETWEEN SUBSTR( paramvalue, 1, 4 ) AND SUBSTR( paramvalue, -4 ) THEN 1 END ) > 0 )
AND ( COUNT( CASE WHEN paramid = 3 THEN 1 END ) = 0
OR COUNT( CASE WHEN paramid = 3 AND paramvalue = 'F' THEN 1 END ) > 0 )
AND ( COUNT( CASE WHEN paramid = 4 THEN 1 END ) = 0
OR COUNT( CASE WHEN paramid = 4 AND paramvalue = 'M' THEN 1 END ) > 0 )
AND ( COUNT( CASE WHEN paramid = 5 THEN 1 END ) = 0
OR COUNT( CASE WHEN paramid = 5 AND paramvalue = 'ISLAMABAD' THEN 1 END ) > 0 )
)
SELECT t.*
FROM tblsegmentparamvalue t
INNER JOIN
segments s
ON ( t.segmentid = s.segmentid )
ORDER BY
t.segmentid,
paramid
Results:
| SEGMENTID | PARAMID | PARAMVALUE |
|-----------|---------|------------|
| 101 | 1 | 4834 |
| 101 | 3 | F |
| 101 | 4 | M |
| 101 | 5 | ISLAMABAD |
This is an example how to get results for three paramters:
select segmentid, paramid, paramvalue
from (
select tpv.*, count(distinct paramid) over (partition by tpv.segmentid) cnt
from tblsegmentparamvalue tpv
join tblsegment ts on ts.segmentid = tpv.segmentid
join tblsegmentparameter tsp on tsp.parameterid = tpv.paramid
where (lower(tsp.paramname) like 'city' and tpv.paramvalue = 'KARACHI')
or (lower(tsp.paramname) like 'gender' and tpv.paramvalue = 'M')
or (lower(tsp.paramname) like 'martialstatus' and tpv.paramvalue = 'S') )
where cnt = 3
SQLFiddle demo
Add rest of parameters (dob, product) as you did it in your query and change last line to where cnt=5 to get full results.

Resources