Query in Oracle for running sum - oracle

I need to pull the result set with sum of the previous record and current record.
Logic
My table is having one key column C1 and a numeric column C2. I need a result like below example. I need 3 columns as the out put out which 1 columns is with running sum. First two columns are same as source with the thrid columns but
The first record of C3 = first record C2.
Second record C3 = "First Record C2 + Second Record C2";
Third record C3 = "First Record C2 + Second Record C2 + Thrid Record C2"
and it should continue for all the records.
Ex.
I have one source table like
C1 C2
---------
a 1
b 2
c 3
I Need output like below
C1 C2 C3
-------------
a 1 1
b 2 3
c 3 6

select c1, c2, sum(c2) over (order by c2) c3
from table_name

Related

Oracle - Transpose (Pivot)

I have a table like this:
ID
C1
C2
C3
1
1
0
1
2
0
1
1
3
1
0
1
and I want to get to this:
ID
Category
Category_Value
1
C1
1
1
C2
0
1
C3
1
2
C1
0
2
C2
1
2
C3
0
3
C1
1
3
C2
0
3
C3
1
How can I do it? (I will add that I need It dynamic, because today I have 40 columns next week I can have 50 columns)
Thank you!
Another dynamic option is to Use UNPIVOT -
SELECT * FROM DATA
UNPIVOT (
Category_Value FOR Category IN (C1 AS 'C1', C2 AS 'C2', C3 AS 'C3')
);
Still you have to specify the column names as a list. If you want a fully dynamic query, You should try doing this at the presentation layer instead of database layer.
Demo.
One straightforward approach uses a union query:
SELECT ID, 'C1' AS Category, C1 AS Category_Value FROM yourTable
UNION ALL
SELECT ID, 'C2', C2 FROM yourTable
UNION ALL
SELECT ID, 'C3', C3 FROM yourTable
ORDER BY ID, Category;

Data manipulation in APEX 20 oracle

Currently I am performing dynamic action (executing server side code) where I am selecting the values from two different tables (XYZ & ABC) performing calculation and inserting into another table (ABC_TEMP) and creating a report view out of that in apex(v20)
Below is what I am performing.
BEGIN
INSERT INTO ABC_TEMP (
A1, --> VARCHAR2(4000)
B1, --> VARCHAR2(4000)
C1, --> NUMBER
D1, --> NUMBER
E1, --> NUMBER
F, --> NUMBER
G1, --> NUMBER
H3, --> NUMBER
I3, --> NUMBER
J, --> NUMBER
K, --> NUMBER
L, --> NUMBER
timestamp -->timestamp(6)
)
VALUES (
:A_SELECT,
:B_SELECT,
:C_SELECT,
(SELECT D2 FROM XYZ WHERE B2 = :B_SELECT AND C2 = :C_SELECT),
(SELECT E2 FROM XYZ WHERE B2 = :B_SELECT AND C2 = :C_SELECT),
(SELECT SUM(D2 + E2) FROM XYZ WHERE B2 = :B_SELECT AND C2 = :C_SELECT),
(SELECT G2 FROM XYZ WHERE B2 = :B_SELECT AND C2 = :B_SELECT),
(SELECT H2 FROM ABC WHERE A2 = :A_SELECT AND P2 = 'mock1' AND SE = 'mock2' AND Q2 = 'val1'),
(SELECT H2 FROM ABC WHERE A2 = :A_SELECT AND P2 = 'mock1' AND SE = 'mock2' AND Q2 = 'val2'),
(:J), --> This value is derived from `ABC_TEMP` table only by dividing I3 BY F
(:K), --> This value is derived from `ABC_TEMP` table only by dividing H3 BY G1
(:L), --> this value is derived from low of J & K column
(CURRENT_TIMESTAMP)
);
END;
My question is how do I set the values of column J,K,L in the same query as it involves selecting from the same table and performing calculation on top of it where I am inserting data.
if this is not possible what can be other approach out here.
Literally copy/paste those columns' source (select statements) and divide them:
values
(:A_SELECT,
...
-- for J, literally copy/paste I3 / F
(SELECT H2 FROM ABC WHERE A2 = :A_SELECT AND P2 = 'mock1' AND SE = 'mock2' AND Q2 = 'val2') /
(SELECT SUM(D2 + E2) FROM XYZ WHERE B2 = :B_SELECT AND C2 = :C_SELECT)
-- the same goes for other columns
);
Does it work? Sure:
SQL> select (select sal from emp where rownum = 1) /
2 (select empno from emp where rownum = 1) as j
3 from dual;
J
----------
,108562899
SQL>
Is it optimized? Of course not, you'll be using the same query twice (once for the "original" column and once for "derived" one).
Can you optimize it? Maybe. Try to create bunch of CTEs (one for each subquery you used) and then re-use it for derived columns.
On the other hand, why would you store such a value into the table? That's redundant. Omit (drop) columns J, K and L from the table and compute their value whenever needed, e.g.
select a1,
c1,
i3 / f as J --> this
from abc_temp
where ...
Or, you could even create a view using the same select (I posted above) and select values from a view.

How to update data by select random row value from another table

I have three tables A, B, C, and I want to randomly take a row from the col_b column of the table B then update it to the table A. Table C is the subtable of Table B, which is used to filter the data of Table B.
Here is my sql statement:
update a a
set a.col_a_b =
(select t.col_b
from (select a1.col_a, b1.col_b, a1.rn_var
-- the number 6 is because I only have 6 rows of data,
-- and the real situation should be the total number of conditions in table b
from (select a0.col_a, TRUNC(dbms_random.value(1, 6)) rn_var
from a a0) a1
left join (select b.col_b, rownum rn
from b b
where exists (select 1
from c c
where b.id = c.col_b_id
and c.col_c = 'c1')) b1
on a1.rn_var = b1.rn) t
where t.col_a = a.col_a);
I found a strange phenomenon:
If I remove a1.rn_var (line from (select a1.col_a, b1.col_b, a1.rn_var), it doesn't work as my expected
On the basis of the above, if I replace exists with left join (or join), the result is the same
If I reomve both a1.rn_var and exists, it will work fine.
I know there may be a better way to implement it, but who can tell me why?
Update:
Actually, it is caused by this sql:
select a1.col_a, b1.col_b -- remove a1.rn_var
from (select a0.col_a, TRUNC(dbms_random.value(1, 6)) rn_var from a a0) a1
left join (select b.col_b, rownum rn
from b b
where exists (select 1
from c c
where b.id = c.col_b_id
and c.col_c = 'c1')) b1
on a1.rn_var = b1.rn
-- this is for better display of results
where a1.col_a = 'a1';
In the above sql, I may get multiple rows of data or column b1.col_b is empty, as shown below:
a1 b1
a1 b2
a1 b4
------------------------------------------------
a1 -- here is null
In addition, each value of column a1.col_a is the same, I mean, if value a1 has multiple rows, then value a2 (and so on) has the same result, like this:
a1 b2
a1 b4
a1 b5
a2 b2
a2 b4
a2 b5
...
You can use a random number and order by that random number to get random records.
I prefer using the following technique:
UPDATE A A
SET
A.COL_A_B = (
SELECT
COL_B
FROM
(
SELECT
COL_B,
TRUNC(DBMS_RANDOM.VALUE(1, COUNT(1) OVER())) RANDOM_NUMBER --GENERATES RANDOM NUMBER
FROM
(
SELECT DISTINCT
B.COL_B -- FETCHING DISTINCT RESULT
FROM
B B
-- EXISTS IS CONVERTED INTO JOIN
JOIN C C ON ( B.ID = C.COL_B_ID
AND C.COL_C = 'c1' )
)
ORDER BY
RANDOM_NUMBER -- ORDERING IS DONE BY RANDOM NUMBER
FETCH FIRST ROWS ONLY -- FETCHING ONLY FIRST ROW FROM ORDERED RECORDS
)
)
Cheers!!

Split last column into two equal halves in unix

I need to split last column into two separate columns & delete some part of it.
Currently all the values in the last column has 6 numbers . I need to split them into two separate columns.
First column should have first three numbers and second column should have next three numbers.
I ultimately want to delete newly created second column.
Data -
ID c1 c2 c3 c4 c5
12 A XY 123 456 657098
The new file should be created as below -
Data 2
ID c1 c2 c3 c4 c5
12 A XY 123 456 657
Thanks
You can use this awk that checks length of last column for each row:
awk 'length($NF) == 6 { $NF = substr($NF, 1, 3) } 1' file
Data -
ID c1 c2 c3 c4 c5
12 A XY 123 456 657

identify at least 1 column having a value + MVC Linq

I'm starting to work with MVC, but i'm struck with one logic with LinQ query. I have attached the image which explains the scenario and logic. Kindly help me with linq query
Column A Column B Column C
Test A A1 C1
Test A A2 C2
Test A A4 C3
Test A A5
Test B B1
Test B B2 C7
Test B B3
Test B B4 C9
Test C D1
Test C D2
Count of (Column A with atleast minimum 1 Column B has Column C value)/(Total of Column A)
Test A 3/5= 0.6
Test B 2/4= 0.5
Test C 0/2= 0
something like that :
Not clear what's impact of column B, by the way...
yourTable.GroupBy(m => m.ColumnA)
.Select(m=> new {
key = m.Key,
count = m.Count(x => x.ColumnC == null) / (decimal)m.Count()
});

Resources