how to get if value not in oracle group by - oracle

I have a table named statustimeline which has structure as
id | applicationid | status | createdon
---+---------------+--------+----------
11 |1 |4 |
---+---------------+--------+----------
10 |1 |3 |
---+---------------+--------+----------
9 |1 |2 |
---+---------------+--------+----------
8 |1 |1 |
---+---------------+--------+----------
7 |2 |3 |
---+---------------+--------+----------
6 |2 |2 |
---+---------------+--------+----------
5 |2 |1 |
---+---------------+--------+----------
4 |3 |5 |
---+---------------+--------+----------
3 |3 |3 |
---+---------------+--------+----------
2 |3 |2 |
---+---------------+--------+----------
1 |3 |1 |
---+---------------+--------+----------
if I partition it on application id, there will be three groups.
select applicationid,ngstatus, row_number() over (partition by applicationid order by id desc) rownumbr
from applicationstatustimeline ;
i want to select only those applicationid whose group never had status=4 (applicationid = 2 and applicationid = 3 in this case)
is there any function?

Why not a simple minus set operator?
SQL> with statustimeline (id, applicationid, status) as
2 -- sample data
3 (select 11, 1, 4 from dual union all
4 select 10, 1, 3 from dual union all
5 select 9, 1, 2 from dual union all
6 select 8, 1, 1 from dual union all
7 select 7, 2, 3 from dual union all
8 select 6, 2, 2 from dual union all
9 select 5, 2, 1 from dual union all
10 select 4, 3, 5 from dual union all
11 select 3, 3, 3 from dual union all
12 select 2, 3, 2 from dual union all
13 select 1, 3, 1 from dual
14 )
15 -- query you need
16 select applicationid
17 from statustimeline
18 minus
19 select applicationid
20 from statustimeline
21 where status = 4;
APPLICATIONID
-------------
2
3
SQL>

If you want to use analytic functions here, we can use COUNT:
WITH cte AS (
SELECT t.*,
COUNT(CASE WHEN status = 4 THEN 1 END) OVER (PARTITION BY applicationid) AS four_cnt
FROM applicationstatustimeline t
)
SELECT id, applicationid, status, createdon
FROM cte
WHERE four_cnt = 0;
In the CTE above, we generate a computed column four_cnt, whose value would be identical for every group of applicationid records. That value would be zero assuming status=4 never occurs, otherwise it would be some value greater than zero.

Yet other option is to use the NOT EXISTS as follows:
SELECT *
FROM STATUSTIMELINE S
WHERE S.STATUS <> 4
AND NOT EXISTS (
SELECT 1
FROM STATUSTIMELINE S4
WHERE S4.STATUS = 4
AND S.APPLICATIONID = S4.APPLICATIONID
);
Yet another option is to use the NOT IN as follows:
SELECT *
FROM STATUSTIMELINE S
WHERE S.STATUS <> 4
AND S.APPLICATIONID NOT IN (
SELECT S4.APPLICATIONID
FROM STATUSTIMELINE S4
WHERE S4.STATUS = 4
)

Related

Want to convert rows to columns

I have one table t as below
Customer _no | receipt_no
----------------------------
A | 123
A. | 234
A. | 345
B. | 465
B. | 675
I want result as
Customer _no | receipt_1 | receipt_2
A. | 123. | 234
A. | 345. | Null
B. | 465. | 675
Please suggest how to do this
If I understood it correctly, you want to have two columns for each customer. If that's so, here's one option.
SQL> with test (cno, rno) as
2 (select 'A', 123 from dual union all
3 select 'A', 234 from dual union all
4 select 'A', 345 from dual union all
5 select 'A', 444 from dual union all
6 select 'A', 555 from dual union all
7 select 'B', 456 from dual union all
8 select 'B', 675 from dual
9 ),
10 inter as
11 (select cno, rno,
12 row_number() over (partition by cno order by cno, rno) rn,
13 round(row_number() over (partition by cno order by cno, rno) /2) grp
14 from test
15 )
16 select cno,
17 max(decode(mod(rn, 2), 1, rno)) r1,
18 max(decode(mod(rn, 2), 0, rno)) r2
19 from inter
20 group by cno, grp
21 order by cno, grp;
C R1 R2
- ---------- ----------
A 123 234
A 345 444
A 555
B 456 675
SQL>

Oracle Hierarchical query with multiple nodes

I have a View and a Table that has the following details:
V_Mgmt
DMId PMId TLId
1 1 1
1 1 2
1 2 3
2 3 4
2 3 5
2 4 6
T_ProjLevels
TLId DevId ParentDevId
1 1 0
1 2 1
1 3 1
2 4 0
2 5 4
2 6 4
2 7 6
3 8 0
3 9 0
4 10 0
4 11 0
4 12 11
Ideally, my tree structure would be as per the left image. But, I am required to create a tree structure as per the right image by skipping the TL's.
So far, I was successfully able to create my tree with only DevId's by using the below query. Need some help in creating this new tree structure.
SELECT DevId,ParentDevId from T_ProjLevels
START WITH ParentDevId=0
Connect By Nocycle Prior "DevId" = "ParentDevId"
ORDER SIBLINGS BY ParentDevId
Look at your data structure. You have one row representing DMID 1, PMID 1, which you want to show as two separate rows in your tree. Somehow, you need to create extra rows in your result set to represent these intermediate nodes. A good way to do that is using GROUPING SETS.
So, what we will do is join the two tables and GROUP BY GROUPING SETS(...) in such a way as to get a merged hierarchy of all the nodes, whether they are DM, PM, or DEV nodes.
Once we have that, we will just do a simple CONNECT BY query on the merged hierarchy.
I am pasting in a working implementation of that plan below, but first let me just say that if you need to do stuff like this often, seriously consider that maybe your data model is not properly designed.
Here is the query, with comments. This version will give you the tree on the right in your post: that is, the one that omits the TL levels. A simple modification would give the tree on the left instead... it was unclear which one you were really after (sorry).
-- First provide data to simulate your V_Mgmt table...
With V_Mgmt ( DMid, PMId, TLId ) AS (
SELECT 1, 1, 1 FROM DUAL UNION ALL
SELECT 1, 1, 2 FROM DUAL UNION ALL
SELECT 1, 2, 3 FROM DUAL UNION ALL
SELECT 2, 3, 4 FROM DUAL UNION ALL
SELECT 2, 3, 5 FROM DUAL UNION ALL
SELECT 2, 4, 6 FROM DUAL ),
-- ... and your T_ProjLevels table
T_ProjLevels (TLId, DevId, ParentDevId) AS (
SELECT 1, 1, 0 FROM DUAL UNION ALL
SELECT 1, 2, 1 FROM DUAL UNION ALL
SELECT 1, 3, 1 FROM DUAL UNION ALL
SELECT 2, 4, 0 FROM DUAL UNION ALL
SELECT 2, 5, 4 FROM DUAL UNION ALL
SELECT 2, 6, 4 FROM DUAL UNION ALL
SELECT 2, 7, 6 FROM DUAL UNION ALL
SELECT 3, 8, 0 FROM DUAL UNION ALL
SELECT 3, 9, 0 FROM DUAL UNION ALL
SELECT 4, 10, 0 FROM DUAL UNION ALL
SELECT 4, 11, 0 FROM DUAL UNION ALL
SELECT 4, 12, 11 FROM DUAL ),
-- Next, merge them together
-- (A) using GROUPING_SETS to create extra rows for the DM, PM, but not the TL-level nodes
-- (B) combining DM, PM, (but not TL), and DEV ids into a common set of "node", "parent_node", and "node_name" columns
merged_hierarchy ( node, parent_node, node_name ) AS (
SELECT rtrim(m.dmid || '.' || m.pmid || '.' || pl.devid,'.') node,
rtrim(
case
when grouping(m.pmid) = 1 then NULL
when grouping(m.tlid) = 1 then to_char(m.dmid)
when grouping(pl.devid) = 1 then m.dmid || '.' || m.pmid
else
m.dmid || '.' || m.pmid || '.' || nullif(pl.parentdevid,0) end,'.') parent_node,
case when grouping(pl.devid) = 0 THEN 'DEV' || pl.devid
when grouping(m.tlid) = 0 THEN 'TL' || m.tlid
when grouping(m.pmid) = 0 THEN 'PM' || m.pmid
when grouping(m.dmid) = 0 THEN 'DM' || m.dmid
end node_name
from v_mgmt m
left join t_projlevels pl on pl.tlid = m.tlid
group by grouping sets ( ( m.dmid ), ( m.dmid, m.pmid), (m.dmid, m.pmid, m.tlid, pl.devid, pl.parentdevid ) )
)
-- Finally, query the merged hierarchy as a straight-forward CONNECT BY query
SELECT lpad(' ',5*(level-1),' ') || node_name output
FROM merged_hierarchy
START WITH parent_node IS NULL
CONNECT BY parent_node = prior node
-- Exclude the outer-joined rows from T_ProjLevels...
AND node_name != 'DEV';
+--------------------------+
| OUTPUT |
+--------------------------+
| DM1 |
| PM1 |
| DEV1 |
| DEV2 |
| DEV3 |
| DEV4 |
| DEV5 |
| DEV6 |
| DEV7 |
| PM2 |
| DEV8 |
| DEV9 |
| DM2 |
| PM3 |
| DEV10 |
| DEV11 |
| DEV12 |
| PM4 |
+--------------------------+

Oracle add group function over result rows

I'm trying to add an aggregate function column to an existing result set. I've tried variations of OVER(), UNION, but cannot find a solution.
Example current result set:
ID ATTR VALUE
1 score 5
1 score 7
1 score 9
Example desired result set:
ID ATTR VALUE STDDEV (score)
1 score 5 2
1 score 7 2
1 score 9 2
Thank you
Seems like you're after:
stddev(value) over (partition by attr)
stddev(value) over (partition by id, attr)
It just depend on what you need to partition by. Based on sample data the attr should be enough; but I could see possibly the ID and attr.
Example:
With CTE (ID, Attr, Value) as (
SELECT 1, 'score', 5 from dual union all
SELECT 1, 'score', 7 from dual union all
SELECT 1, 'score', 9 from dual union all
SELECT 1, 'Z', 1 from dual union all
SELECT 1, 'Z', 5 from dual union all
SELECT 1, 'Z', 8 from dual)
SELECT A.*, stddev(value) over (partition by attr)
FROM cte A
ORDER BY attr, value
DOCS show that by adding an order by to the analytic, one can acquire the cumulative standard deviation per record.
Giving us:
+----+-------+-------+------------------------------------------+
| ID | attr | value | stdev |
+----+-------+-------+------------------------------------------+
| 1 | Z | 1 | 3.51188458428424628280046822063322249225 |
| 1 | Z | 5 | 3.51188458428424628280046822063322249225 |
| 1 | Z | 8 | 3.51188458428424628280046822063322249225 |
| 1 | score | 5 | 2 |
| 1 | score | 7 | 2 |
| 1 | score | 9 | 2 |
+----+-------+-------+------------------------------------------+

Get Hierarchy level and all node references on Oracle

I have been reading about CONNECT BY and CTE in Oracle, but I can't come up with a solution. I don't know how to use properly CONNECT BY to my needs, and recursive CTE's in Oracle are limited to 2 branches(one UNION ALL) and I'm using 3 branches.
In SQL Server it was kind of easy after I found this article. I only added another UNION ALL regarding to return all node references.
What I trying to do is having a hierarchy like this:
Code|Father
1 |NULL
2 |1
3 |2
And this should return me:
Node|Father|Level|JumpsToFather
1 |1 |1 |0
2 |1 |2 |1
2 |2 |2 |0
3 |1 |3 |2
3 |2 |3 |1
3 |3 |3 |0
Note: Yes I need to return a reference to themselves counting as zero jumps on the hierarchy
Here is a solution using a recursive CTE. I used lvl as column header since level is a reserved word in Oracle. You will see other differences in terminology as well. I use "parent" for the immediately higher level and "ancestor" for >= 0 steps (to accommodate your requirement of showing a node as its own ancestor). I used an ORDER BY clause to cause the output to match yours; you may or may not need the rows ordered.
Your question stimulated me to read again, in more detail, about hierarchical queries, to see if this can be done with them instead of recursive CTEs. Actually I already know you can, by using CONNECT_BY_PATH, but using a substr on that just to retrieve the top level in a hierarchical path is not satisfying at all, there must be a better way. (If that was the only way to do it with hierarchical queries, I would definitely go the recursive CTE route if it was available). I will add the hierarchical query solution here, if I can find a good one.
with h ( node, parent ) as (
select 1 , null from dual union all
select 2 , 1 from dual union all
select 3 , 2 from dual
),
r ( node , ancestor, steps ) as (
select node , node , 0
from h
union all
select r.node, h.parent, steps + 1
from h join r
on h.node = r.ancestor
)
select node, ancestor,
1+ (max(steps) over (partition by node)) as lvl, steps
from r
where ancestor is not null
order by lvl, steps desc;
NODE ANCESTOR LVL STEPS
---------- ---------- ---------- ----------
1 1 1 0
2 1 2 1
2 2 2 0
3 1 3 2
3 2 3 1
3 3 3 0
Added: Hierarchical query solution
OK - found it. Please test both solutions to see which performs better; from tests on a different setup, recursive CTE was quite a bit faster than hierarchical query, but that may depend on the specific situation. ALSO: recursive CTE works only in Oracle 11.2 and above; the hierarchical solution works with older versions.
I added a bit more test data to match Anatoliy's.
with h ( node, parent ) as (
select 1 , null from dual union all
select 2 , 1 from dual union all
select 3 , 2 from dual union all
select 4 , 2 from dual union all
select 5 , 4 from dual
)
select node,
connect_by_root node as ancestor,
max(level) over (partition by node) as lvl,
level - 1 as steps
from h
connect by parent = prior node
order by node, ancestor;
NODE ANCESTOR LVL STEPS
---------- ---------- ---------- ----------
1 1 1 0
2 1 2 1
2 2 2 0
3 1 3 2
3 2 3 1
3 3 3 0
4 1 3 2
4 2 3 1
4 4 3 0
5 1 4 3
5 2 4 2
5 4 4 1
5 5 4 0
thx for question, i spent 1 hour to write this:
with t as ( select code, parent, level l
from (select 1 as code, NULL as parent from dual union
select 2 , 1 from dual union
select 3 , 2 from dual
-- add some more data for demo case
union
select 4 , 2 from dual union
select 5 , 4 from dual
)
start with parent is null
connect by prior code = parent )
select code, (select code
from t t1
where l = ll
and rownum = 1
start with t1.code = main_t.code
connect by prior t1.parent = t1.code
) parent,
l code_level,
jumps
from (
select distinct t.*, l-level jumps, level ll
from t
connect by level <= l
) main_t
order by code, parent
as you can see, i'am add some more data to test my sql, here is output
CODE PARENT CODE_LEVEL JUMPS
---------- ---------- ---------- ----------
1 1 1 0
2 1 2 1
2 2 2 0
3 1 3 2
3 2 3 1
3 3 3 0
4 1 3 2
4 2 3 1
4 4 3 0
5 1 4 3
5 2 4 2
5 4 4 1
5 5 4 0
13 rows selected

update row without considering space between two words

I am looking to replace text in column Text with format(A,B) where text contains A and B only and ignoring spaces between A and B.
Here is test data
Id Text
1 A B //should be replaced with format(A,B).
2 A B //should be replaced with format(A,B).
3 A B //should be replaced with format(A,B).
4 A 1 B //shouldn't be replaced.
5 A B 1 //should be replaced with format(A,B) 1.
I think I have to do something like
UPDATE test SET text = REPLACE(text, 'A[wild char for space]B', 'format(A,B)');
but how should I compare only for space? like % will compare everything.
You can use Oracle Regex fro this
UPDATE test SET text = REGEXP_REPLACE(testcol, '(A[:blank:]B)', 'format(A,B)')
Oracle Regex
Just a hint:
SQL> with t as (
2 select 'A B' col from dual union all
3 select 'A B' from dual union all
4 select 'A B' from dual union all
5 select 'AB' from dual union all
6 select 'A 1 B' from dual union all
7 select 'A B 1' from dual
8 )
9 select regexp_replace(t.col, 'A[[:space:]]*B', 'format(A,B)') from t
10 /
REGEXP_REPLACE(T.COL,'A[[:SPACE:]]*B','FORMAT(A,B)')
--------------------------------------------------------------------------------
format(A,B)
format(A,B)
format(A,B)
format(A,B)
A 1 B
format(A,B) 1
Sql for your scenario:
with tab(Id,Text) as
(select 1,'A B' from dual union all -- //should be replaced with format(A,B).
select 2,'B C' from dual union all -- //should be replaced with format(A,B).
select 3,'A B' from dual union all -- //should be replaced with format(A,B).
select 4,'A 2 B' from dual union all -- //shouldn't be replaced.
select 5,'Y Z 1' from dual) -- //should be replaced with format(A,B) 1.
select id,
text,
regexp_replace(text, '^\D\s+\D', 'format('||regexp_substr(text, '\D')||','||trim(regexp_substr(text, '\D+', 2))||')') format
from tab;
output:
| ID | TEXT | FORMAT |
|----|-------------|---------------|
| 1 | A B | format(A,B) |
| 2 | B C | format(B,C) |
| 3 | A B | format(A,B) |
| 4 | A 2 B | A 2 B |
| 5 | Y Z 1 | format(Y,Z) 1 |
And the update statement becomes
UPDATE test SET text = regexp_replace(text, '^\D\s+\D', 'format('||regexp_substr(text, '\D')||','||trim(regexp_substr(text, '\D+', 2))||')');

Resources