Divide columns - oracle - oracle

Hello/ I need divide this table
I try use this but have bad result
SELECT MIN(y1),
MAX(y1),
MIN(redempamount1),
MAX(redempamount1),
MIN(f1),
MIN(f2),
MAX(f1),
MAX(f2)
FROM (SELECT to_number(y1) y1,
redempamount1,
add_months('02.03.2018', 12 * y1 - 12) f1,
add_months('02.03.2018', 12 * y1) - 1 f2,
row_number() over(PARTITION BY ntl ORDER BY rownum DESC) grp
FROM (SELECT to_number(t.y1) y1,
redempamount1,
add_months('02.03.2018', 12 * y1 - 12) f1,
add_months('02.03.2018', 12 * y1) - 1 f2,
ntile(2) over(ORDER BY rownum DESC) ntl
FROM inspolreport t))
GROUP BY grp;
Y1 REDEMPAMOUNT1 F1 F2
-- ------------- ---------- ----------
1 0 18.12.2008 17.12.2009
2 3362.54 18.12.2009 17.12.2010
3 6030.84 18.12.2010 17.12.2011
4 8873.52 18.12.2011 17.12.2012
5 11830.9 18.12.2012 17.12.2013
6 15041.83 18.12.2013 17.12.2014
7 18382.37 18.12.2014 17.12.2015
8 21857.15 18.12.2015 17.12.2016
9 25472.97 18.12.2016 17.12.2017
10 27359.51 18.12.2017 17.12.2018
11 31690.65 18.12.2018 17.12.2019
12 36195 18.12.2019 17.12.2020
13 40879.06 18.12.2020 17.12.2021
14 45750.5 18.12.2021 17.12.2022
15 53491.44 18.12.2022 17.12.2023
expected output
Y1 REDEMPAMOUNT1 F1 F2 Y2 REDEMPAMOUNT1 F3 F4
-- ------------- ---------- ---------- -- ------------- ---------- ----------
1 0 18.12.2008 17.12.2009 9 25472.97 18.12.2016 17.12.2017
2 3362.54 18.12.2009 17.12.2010 10 27359.51 18.12.2017 17.12.2018
3 6030.84 18.12.2010 17.12.2011 11 31690.65 18.12.2018 17.12.2019
4 8873.52 18.12.2011 17.12.2012 12 36195 18.12.2019 17.12.2020
5 11830.9 18.12.2012 17.12.2013 13 40879.06 18.12.2020 17.12.2021
6 15041.83 18.12.2013 17.12.2014 14 45750.5 18.12.2021 17.12.2022
7 18382.37 18.12.2014 17.12.2015 15 53491.44 18.12.2022 17.12.2023
8 21857.15 18.12.2015 17.12.2016

If what you want to do is split your results into two side-by-side sets of data, then you will need to pivot your data accordingly.
I can see that is what you're trying to do, but you need to use conditional aggregates to produce the columns.
So you want something like:
WITH your_table AS (SELECT 1 y1, 0 redempamount1, to_date('18/12/2008', 'dd/mm/yyyy') f1, to_date('17/12/2009', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 2 y1, 3362.54 redempamount1, to_date('18/12/2009', 'dd/mm/yyyy') f1, to_date('17/12/2010', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 3 y1, 6030.84 redempamount1, to_date('18/12/2010', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 4 y1, 8873.52 redempamount1, to_date('18/12/2011', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 5 y1, 15041.83 redempamount1, to_date('18/12/2012', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 6 y1, 15041.83 redempamount1, to_date('18/12/2013', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 7 y1, 18382.37 redempamount1, to_date('18/12/2014', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 8 y1, 21857.15 redempamount1, to_date('18/12/2015', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 9 y1, 25472.97 redempamount1, to_date('18/12/2016', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 10 y1, 27359.51 redempamount1, to_date('18/12/2017', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 11 y1, 31690.65 redempamount1, to_date('18/12/2018', 'dd/mm/yyyy') f1, to_date('17/12/2011', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 12 y1, 36195 redempamount1, to_date('18/12/2019', 'dd/mm/yyyy') f1, to_date('17/12/2020', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 13 y1, 40879.06 redempamount1, to_date('18/12/2020', 'dd/mm/yyyy') f1, to_date('17/12/2021', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 14 y1, 45750.5 redempamount1, to_date('18/12/2021', 'dd/mm/yyyy') f1, to_date('17/12/2022', 'dd/mm/yyyy') f2 FROM dual UNION ALL
SELECT 15 y1, 53491.44 redempamount1, to_date('18/12/2022', 'dd/mm/yyyy') f1, to_date('17/12/2023', 'dd/mm/yyyy') f2 FROM dual)
SELECT MAX(CASE WHEN grp = 1 THEN y1 END) AS y1,
MAX(CASE WHEN grp = 1 THEN redempamount1 END) AS redempamount1,
MAX(CASE WHEN grp = 1 THEN f1 END) AS f1,
MAX(CASE WHEN grp = 1 THEN f2 END) AS f2,
MAX(CASE WHEN grp = 2 THEN y1 END) AS y2,
MAX(CASE WHEN grp = 2 THEN redempamount1 END) AS redempamount2,
MAX(CASE WHEN grp = 2 THEN f1 END) AS f3,
MAX(CASE WHEN grp = 2 THEN f2 END) AS f4
FROM (SELECT y1,
redempamount1,
f1,
f2,
grp,
row_number() OVER (PARTITION BY grp ORDER BY y1) rn
FROM (SELECT y1,
redempamount1,
f1,
f2,
NTILE(2) OVER (ORDER BY y1) grp
FROM your_table))
GROUP BY rn
ORDER BY rn;
Y1 REDEMPAMOUNT1 F1 F2 Y2 REDEMPAMOUNT2 F3 F4
---------- ------------- ----------- ----------- ---------- ------------- ----------- -----------
1 0 18/12/2008 17/12/2009 9 25472.97 18/12/2016 17/12/2011
2 3362.54 18/12/2009 17/12/2010 10 27359.51 18/12/2017 17/12/2011
3 6030.84 18/12/2010 17/12/2011 11 31690.65 18/12/2018 17/12/2011
4 8873.52 18/12/2011 17/12/2011 12 36195 18/12/2019 17/12/2020
5 15041.83 18/12/2012 17/12/2011 13 40879.06 18/12/2020 17/12/2021
6 15041.83 18/12/2013 17/12/2011 14 45750.5 18/12/2021 17/12/2022
7 18382.37 18/12/2014 17/12/2011 15 53491.44 18/12/2022 17/12/2023
8 21857.15 18/12/2015 17/12/2011
However, if you are in 11g and above, you can use the PIVOT command, which I would argue makes your intentions clearer:
SELECT o_y1 AS y1,
o_redempamount1 AS redempamount1,
o_f1 AS f1,
o_f2 AS f2,
t_y1 AS y2,
t_redempamount1 AS redempamount2,
t_f1 AS f3,
t_f2 AS f4
FROM (SELECT y1,
redempamount1,
f1,
f2,
grp,
row_number() OVER (PARTITION BY grp ORDER BY y1) rn
FROM (SELECT y1,
redempamount1,
f1,
f2,
NTILE(2) OVER (ORDER BY y1) grp
FROM your_table))
PIVOT (MAX(y1) AS y1, MAX(redempamount1) AS redempamount1, MAX(f1) AS f1, MAX(f2) AS f2
FOR grp IN (1 AS o, 2 AS t))
ORDER BY rn;

Related

How to get the data from oracle on the following demand?

The table like this.
bh sl productdate
a1 100 2022-1-1
a1 220 2022-1-2
a1 220 2022-1-3
a2 200 2022-1-1
a2 350 2022-1-2
a2 350 2022-1-3
The result like this.
bh sl_q(sl_before) sl_h(sl_after) sl_b(changeValue) productdate
a1 100 220 120 2022-1-2
a2 200 350 150 2022-1-2
Rules:the same field bh, when the field sl change,then get the record.
We can use a ROW_NUMBER trick here:
WITH cte AS (
SELECT t.*, ROW_NUMBER() OVER (PARTITION BY bh ORDER BY productdate) rn1,
ROW_NUMBER() OVER (PARTITION BY bh ORDER BY productdate DESC) rn2
FROM yourTable t
)
SELECT bh, MAX(CASE WHEN rn1 = 1 THEN sl END) AS sl_q,
MAX(CASE WHEN rn2 = 1 THEN sl END) AS sl_h,
MAX(CASE WHEN rn2 = 1 THEN sl END) -
MAX(CASE WHEN rn1 = 1 THEN sl END) AS sl_b
FROM cte
GROUP BY bh;

(Oracle)Getting 25th number using interpolating

My goal is to get 25th number. For instance I have 4 row, such as 3,4,5 and 7.
My goal is to get 1.25th(=(4+1)0.25).
Expected result is 3.25 which is obtained by interpolating(3+0.25(4-3)).
I have tried as below.
But is there any other efficient way?
WITH DATASET AS (
SELECT 3 C1 FROM DUAL
UNION
SELECT 4 FROM DUAL
UNION
SELECT 5 FROM DUAL
UNION
SELECT 7 FROM DUAL
)
SELECT
--RNK, C1, NEXTC1-C1, FIRSTQLOCAION, FIRSTQLOCAION-RNK, C1+(NEXTC1-C1)*(FIRSTQLOCAION-RNK)
C1+(NEXTC1-C1)*(FIRSTQLOCAION-RNK)
FROM(
SELECT C1,
LEAD(C1, 1) OVER (ORDER BY C1) as NEXTC1 ,
RANK() OVER (ORDER BY C1) AS RNK,
((SUM(1) OVER (PARTITION BY NULL)) +1) * 0.25 AS FIRSTQLOCAION
FROM DATASET
)
WHERE
FIRSTQLOCAION>=RNK AND FIRSTQLOCAION<=RNK+1;
You can use analytical function as follows:
Select c1,
c1 + (
(((Count(1) over () + 1)*0.25) - 1) * (lead(c1) over (order by c1) - c1)
) as calculated_number from
From your_table t
In this solution last record will have calculated value null as lead value will be null and you will have to adjust its value as per your requirement.
If your expectation is a single number from query then usw following:
Select min(c1) +
0.25 * (min(case when rn = 2 then c1 end)
- min(case when rn = 1 then c1 end)) as calculated_number
from
(Select t.*,
Row_number() over (order by c1)
From t)
WITH t AS (
SELECT 3 C1 FROM DUAL
UNION
SELECT 4 FROM DUAL
UNION
SELECT 5 FROM DUAL
UNION
SELECT 7 FROM DUAL
)
SELECT rn,location, calculated
FROM (
Select rn, c1,
C1 +(Count(1) over () + 1)*0.25
-trunc( (Count(1) over () + 1)*0.25 ) *(lead(c1) over (order by c1) - c1) as calculated, --
trunc( (Count(1) over () + 1)*0.25 ) as location --
From (Select t.*, Row_number() over (order by c1) rn From t) ) WHERE rn=location;

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.

How to convert two or more rows into columns in Oracle

I have a table mapping that has Column names and values as shown below. (I have more than 20 columns).
Original Data
I want to convert this into:
(0/NULL)
Final Result
Can anyone please help here.
I have written the below query which worked for only one column but how to do it for more than two-columns?
select * from cte3
pivot(
min(1) for column1 in ( a , b , c , d )
)
order by id
You need to use the conditional aggregation as it can not be handled by PIVOT.
SQL> WITH CTE(ID,COLUMN1,COLUMN2,COLUMN3,COLUMN4) AS
2 (SELECT 1,'A','E','I','M' FROM DUAL UNION ALL
3 SELECT 2,'B','F','J','N' FROM DUAL UNION ALL
4 SELECT 3,'C','G','K','O' FROM DUAL UNION ALL
5 SELECT 4,'D','H','L','P' FROM DUAL)
6 --your query starts from here
7 SELECT ID,
8 COALESCE(SUM(CASE WHEN 'A' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS A,
9 COALESCE(SUM(CASE WHEN 'B' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS B,
10 COALESCE(SUM(CASE WHEN 'C' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS C,
11 COALESCE(SUM(CASE WHEN 'D' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS D,
12 COALESCE(SUM(CASE WHEN 'E' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS E,
13 COALESCE(SUM(CASE WHEN 'F' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS F,
14 COALESCE(SUM(CASE WHEN 'G' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS G,
15 COALESCE(SUM(CASE WHEN 'H' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS H,
16 COALESCE(SUM(CASE WHEN 'I' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS I,
17 COALESCE(SUM(CASE WHEN 'J' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS J,
18 COALESCE(SUM(CASE WHEN 'K' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS K,
19 COALESCE(SUM(CASE WHEN 'L' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS L,
20 COALESCE(SUM(CASE WHEN 'M' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS M,
21 COALESCE(SUM(CASE WHEN 'N' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS N,
22 COALESCE(SUM(CASE WHEN 'O' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS O,
23 COALESCE(SUM(CASE WHEN 'P' IN (COLUMN1,COLUMN2,COLUMN3,COLUMN4) THEN 1 END),0) AS P
24 FROM CTE
25 GROUP BY ID;
ID A B C D E F G H I J K L M N O P
---------- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0
2 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0
4 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1
3 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0
SQL>
with q1 as (
select 1 id, 'A' column1, 'E' column2, 'I' column3, 'M' column4 from dual
union all
select 2 id, 'B' column1, 'F' column2, 'J' column3, 'N' column4 from dual
union all
select 3 id, 'C' column1, 'G' column2, 'K' column3, 'O' column4 from dual
union all
select 4 id, 'D' column1, 'H' column2, 'L' column3, 'P' column4 from dual
),
--select * from q1;
q2 as (
select
id,
column1 as c1, case when column1 in ('A','B','C','D') then id end as c1p,
column2 as c2, case when column2 in ('E','F','G','H') then 4+id end as c2p,
column3 as c3, case when column3 in ('I','J','K','L') then 8+id end as c3p,
column4 as c4, case when column4 in ('M','N','O','P') then 12+id end as c4p
from q1
order by 1
),
--select * from q2;
q3 as (
select id, c1p as pos, c1 as val from q2
union all
select id, c2p as pos, c2 as val from q2
union all
select id, c3p as pos, c3 as val from q2
union all
select id, c4p as pos, c4 as val from q2
order by 1,2
),
--select * from q3;
q4 as (
select distinct pos, val from q3 order by 1
),
--select * from q4;
q5 as (
select q3.id, q4.pos, q4.val, case when q3.val = q4.val then 1 else 0 end as nval
from q3
partition by (q3.id)
right outer join q4
on (q3.pos = q4.pos)
order by 1,2
)
--select * from q5;
select
id,
sum(A) as A, sum(B) as B, sum(C) as C, sum(D) as D,
sum(E) as E, sum(F) as F, sum(G) as G, sum(H) as H,
sum(I) as I, sum(J) as J, sum(K) as K, sum(L) as L,
sum(M) as M, sum(N) as N, sum(O) as O, sum(P) as P
from q5
pivot ( sum(nval) for val in (
'A' as A,'B' as B,'C' as C,'D' as D,
'E' as E,'F' as F,'G' as G,'H' as H,
'I' as I,'J' as J,'K' as K,'L' as L,
'M' as M,'N' as N,'O' as O,'P' as P
)
)
group by id
order by 1
;
Snapshot of Results as expected

Applying Oracle Pivot to get horizentle data

(I am new to Oracle pivot.)
The result of my code is as follow.
ROWNUM C0 M SS
------- --- ---------- ---------- ----------
1 a a__ 3.5
2 a abd 1.5
3 a abe 3.5
4 a ace 5.5
5 b a__ 35
6 b abd 15
7 b abe 35
8 b ace 55
Items in C0 shows vertically as expected.
Now,The purpose is to show the values in C0 horizently.
How to modify my code to make result as follow?
ROWNUM M a b
---------- --------- ---------- ----------
1 a__ 3.5 35
2 abd 1.5 15
3 abe 3.5 35
4 ace 5.5 55
My code is as below;
CREATE TABLE T4 (
C0 VARCHAR2(10),
C1 VARCHAR2(10),
C2 NUMBER
);
INSERT INTO T4 VALUES ('a','abd',1);
INSERT INTO T4 VALUES ('a','abd',2);
INSERT INTO T4 VALUES ('a','abe',3);
INSERT INTO T4 VALUES ('a','abe',4);
INSERT INTO T4 VALUES ('a','ace',5);
INSERT INTO T4 VALUES ('a','ace',6);
INSERT INTO T4 VALUES ('b','abd',10);
INSERT INTO T4 VALUES ('b','abd',20);
INSERT INTO T4 VALUES ('b','abe',30);
INSERT INTO T4 VALUES ('b','abe',40);
INSERT INTO T4 VALUES ('b','ace',50);
INSERT INTO T4 VALUES ('b','ace',60);
SELECT ROWNUM,rr.C0,rr.M, rr.ss
FROM
(
SELECT C0,C1 M, AVG(C2) ss FROM T4 GROUP BY C0, C1
UNION
SELECT C0,SUBSTR(C1,1,1)||'__' ,AVG(C2) ss FROM T4 GROUP BY C0,SUBSTR(C1,1,1) ) rr
ORDER BY rr.C0,rr.M ASC;
You can achieve the result in two ways.
Using PIVOT and using a traditional technique (CASE WHEN).
PIVOT is the recommended technique to use. But I have written both just for your reference.
-- Using PIVOT --
WITH DATAA AS (
SELECT ROWNUM,rr.C0,rr.M, rr.ss
FROM
(
SELECT C0,C1 M, AVG(C2) ss FROM T4 GROUP BY C0, C1
UNION
SELECT C0,SUBSTR(C1,1,1)||'__' ,AVG(C2) ss FROM T4 GROUP BY C0,SUBSTR(C1,1,1) ) rr
)
-- USING PIVOT
SELECT
ROWNUM,
TAB.*
FROM
(
SELECT
*
FROM
(
SELECT
M,
C0,
SS
FROM
DATAA
) PIVOT (
SUM ( SS )
FOR ( C0 )
IN ( 'a' AS A, 'b' AS B )
)
ORDER BY
1
) TAB;
-- Using Traditional technique --
WITH DATAA AS (
SELECT ROWNUM,rr.C0,rr.M, rr.ss
FROM
(
SELECT C0,C1 M, AVG(C2) ss FROM T4 GROUP BY C0, C1
UNION
SELECT C0,SUBSTR(C1,1,1)||'__' ,AVG(C2) ss FROM T4 GROUP BY C0,SUBSTR(C1,1,1) ) rr
)
-- TRADITIONAL WAY
SELECT
ROWNUM,
M,
A,
B
FROM
(
SELECT
M,
MAX(CASE
WHEN C0 = 'a' THEN SS
END) AS A,
MAX(CASE
WHEN C0 = 'b' THEN SS
END) AS B
FROM
DATAA
GROUP BY
M
ORDER BY
M
);
-- Output --
db<>fiddle demo
Cheers!!

Resources