oracle ROW_NUMBER() - oracle

I would like to write a query to display this kind of results
column0
column1
column2
column3
a
x
1
1
a
y
1
2
a
z
1
3
b
x
2
1
b
y
2
2
c
x
3
1
c
y
3
2
c
z
3
3
column2 -> row number of column0
column3 -> row number of partition column0 & column1
I have tried this but not working
SELECT ROW_NUMBER() OVER (PARTITION BY column0 ORDER BY column0 ) column2,
ROW_NUMBER() OVER (PARTITION BY column0 , column1 ORDER BY column0 , column1) column3
FROM DUAL
have you got any ideas ?

Use DENSE_RANK for column2 and ROW_NUMBER for column3:
SELECT
column0,
column1,
DENSE_RANK() OVER (ORDER BY column0) AS column3,
ROW_NUMBER() OVER (PARTITION BY column0 ORDER BY column1) AS column4
FROM yourTable;

With row_number:
SQL> with test (col0, col1) as
2 (select 'a', 'x' from dual union all
3 select 'a', 'y' from dual union all
4 select 'a', 'z' from dual union all
5 select 'b', 'x' from dual union all
6 select 'b', 'y' from dual
7 )
8 select col0, col1,
9 row_number() over (partition by col1 order by col0) col2,
10 row_number() over (partition by col0 order by col1) col3
11 from test
12 order by col0, col1
13 /
COL0 COL1 COL2 COL3
----- ------ ---------- ----------
a x 1 1
a y 1 2
a z 1 3
b x 2 1
b y 2 2
SQL>

Related

Get Ultimate manager oracle

I have data in the below format.
Emp to_location from_location Vehicle
---------------------------------------------
1 A B Road
1 B C Ship
1 C D Air
1 X D Bus
Need the output as
Emp ToL FromL Vehicle
--------------------------
1 A D Air
I tried using Connect by and Start with but the result is coming up as below.
Emp FromL ToL Path
--------------------------
1 B C Air
I need the output as
Emp FromL ToL Path
--------------------------
1 D A Air
The query I have built is like below.
with t as
( select 1 emp, 'A' tloc, 'B' floc, 'Road' v from dual union all
select 1 emp,'B' tloc, 'C' floc, 'Ship' v from dual union all
select 1 emp,'C' tloc, 'D' floc, 'Air' v from dual union all
select 1 emp,'X' tloc, 'D' floc, 'Bus' v from dual
)
select emp,
connect_by_root floc from_loc,
tloc to_location,
v,
CONNECT_BY_ISLEAF ch
from T
where CONNECT_BY_ISLEAF=1
CONNECT BY nocycle prior floc= tloc and prior emp=emp
AND PRIOR SYS_GUID() IS NOT NULL
START WITH tloc ='A'
Can anyone correct the minor thing that I am missing to get the correct output? TIA
I think your query is fine - you just to pick up the root element as you go
SQL> with t as
2 ( select 1 emp, 'A' floc, 'B' tloc, 'Road' v from dual union all
3 select 1 emp,'B' floc, 'C' tloc, 'Ship' v from dual union all
4 select 1 emp,'C' floc, 'D' tloc, 'Air' v from dual union all
5 select 1 emp,'X' floc, 'D' tloc, 'Bus' v from dual
6 )
7 select t.*, level
8 from T
9 where emp=1
10 CONNECT BY nocycle floc= PRIOR tloc and prior emp=emp
11 START WITH floc ='A';
EMP F T V LEVEL
---------- - - ---- ----------
1 A B Road 1
1 B C Ship 2
1 C D Air 3
to then yield
SQL> with t as
2 ( select 1 emp, 'A' floc, 'B' tloc, 'Road' v from dual union all
3 select 1 emp,'B' floc, 'C' tloc, 'Ship' v from dual union all
4 select 1 emp,'C' floc, 'D' tloc, 'Air' v from dual union all
5 select 1 emp,'X' floc, 'D' tloc, 'Bus' v from dual
6 )
7 select emp,
8 connect_by_root floc from_loc,
9 tloc to_location,
10 ltrim(sys_connect_by_path(v,'-'),'-') path
11 from T
12 where emp=1 and CONNECT_BY_ISLEAF=1
13 CONNECT BY nocycle floc= PRIOR tloc and prior emp=emp
14 START WITH floc ='A';
EMP F T PATH
---------- - - ------------------------------
1 A D Road-Ship-Air

oracle sql split string to rows (alphabet in sequential order) - Oracle SQL

I am looking for a oracle sql solution for the below problem:
Here's one option (as union of 3 different cases).
Sample data:
SQL> with test (col) as
2 (select 'A-D' from dual union all
3 select 'J-K' from dual union all
4 select 'X-20' from dual union all
5 select 'XX-20542' from dual union all
6 select 'A/B' from dual union all
7 select 'J/K' from dual
8 )
Query begins here:
9 -- A-D option:
10 select col,
11 chr(ascii(substr(col, 1, 1)) + column_value - 1) res
12 from test cross join
13 table(cast(multiset(select level from dual
14 connect by level <= ascii(substr(col, -1)) - ascii(substr(col, 1, 1)) + 1
15 ) as sys.odcinumberlist))
16 where regexp_like(col, '[[:alpha:]]-[[:alpha:]]')
17 -- XX-20542 option:
18 union all
19 select col, 'No action / ignore'
20 from test
21 where regexp_like(col, '[[:alpha:]]+-\d+')
22 -- A/B option:
23 union all
24 select col,
25 regexp_substr(col, '[[:alpha:]]', 1, column_value)
26 from test cross join
27 table(cast(multiset(select level from dual
28 connect by level <= ascii(substr(col, -1)) - ascii(substr(col, 1, 1)) + 1
29 ) as sys.odcinumberlist))
30 where regexp_like(col, '[[:alpha:]]/[[:alpha:]]');
Result:
COL RES
-------- ------------------
A-D A
A-D B
A-D C
A-D D
J-K J
J-K K
X-20 No action / ignore
XX-20542 No action / ignore
A/B A
A/B B
J/K J
J/K K
12 rows selected.
SQL>
You can use a single recursive query:
WITH ranges (value, min_value, skip, max_value) AS (
SELECT value,
REGEXP_SUBSTR(value, '^([A-Z])([[:punct:]])([A-Z])$', 1, 1, 'i', 1),
CASE REGEXP_SUBSTR(value, '^([A-Z])([[:punct:]])([A-Z])$', 1, 1, 'i', 2)
WHEN '-'
THEN 0
ELSE 1
END,
REGEXP_SUBSTR(value, '^([A-Z])([[:punct:]])([A-Z])$', 1, 1, 'i', 3)
FROM table_name
UNION ALL
SELECT value,
CASE skip
WHEN 0
THEN CHR(ASCII(min_value) + 1)
ELSE max_value
END,
skip,
max_value
FROM ranges
WHERE min_value < max_value
)
SEARCH DEPTH FIRST BY value SET rn
SELECT value,
COALESCE(min_value, 'ignore') AS result
FROM ranges
Which, for the sample data:
CREATE TABLE table_name (value) AS
SELECT 'A-D' FROM DUAL UNION ALL
SELECT 'J-K' FROM DUAL UNION ALL
SELECT 'XX-Y' FROM DUAL UNION ALL
SELECT 'Y-ZZ' FROM DUAL UNION ALL
SELECT 'B/C' FROM DUAL UNION ALL
SELECT 'E/E' FROM DUAL UNION ALL
SELECT 'F/H' FROM DUAL;
Outputs:
VALUE
RESULT
A-D
A
A-D
B
A-D
C
A-D
D
B/C
B
B/C
C
E/E
E
F/H
F
F/H
H
J-K
J
J-K
K
XX-Y
ignore
Y-ZZ
ignore
db<>fiddle here

Oracle SQL Create Corresponding Labels for Paired Right Left Sequential Numbers with Identifiers

I am trying to pair left right parts in a table. I received an answer for a very similar question pairing left right parts but I now need to create a new column stating the corresponding paired part.
For example I have
ID LR Identifier
1 L B15A
2 R A15C
3 L A15C
4 R A15C
5 L A15C
6 R D5A2
9 R D5A2
10 L E5A6
11 R E5A6
12 L E5A6
13 R E5A6
14 R H9S5
17 L EE5A
18 R EE5A
and I need the query to return
ID LR Identifier Counterhand Counterpart
2 R A15C L 3
3 L A15C R 2
4 R A15C L 5
5 L A15C R 4
10 L E5A6 R 11
11 R E5A6 L 10
12 L E5A6 R 13
13 R E5A6 L 12
17 L EE5A R 18
18 R EE5A L 17
The link to the previous question is Here. Very helpfully answered by #mathguy
You can modify #mathguys previous solution and check if each result is the first or second in a pair:
mod(row_number() over (partition by identifier order by id), 2)
will give you either 0 or 1; and then you can then choose to either lead or lag to get the previous or next hand/ID value:
case mod(row_number() over (partition by identifier order by id), 2)
when 0 then lag(lr) over (partition by identifier order by id)
else lead(lr) over (partition by identifier order by id) end as counterhand
Blatantly copying and extending #mathguy's previous code:
with
test_data ( id, lr, identifier ) as (
select 001, 'L', 'B15A' from dual union all
select 002, 'R', 'A15C' from dual union all
select 003, 'L', 'A15C' from dual union all
select 004, 'R', 'A15C' from dual union all
select 005, 'L', 'A15C' from dual union all
select 006, 'R', 'D5A2' from dual union all
select 009, 'R', 'D5A2' from dual union all
select 010, 'L', 'E5A6' from dual union all
select 011, 'R', 'E5A6' from dual union all
select 012, 'L', 'E5A6' from dual union all
select 013, 'R', 'E5A6' from dual union all
select 014, 'R', 'H9S5' from dual union all
select 017, 'L', 'EE5A' from dual union all
select 018, 'R', 'EE5A' from dual
)
-- end of test data, the solution (SQL query) begins below this line
select id, lr, identifier,
case mod(row_number() over (partition by identifier order by id), 2)
when 0 then lag(lr) over (partition by identifier order by id)
else lead(lr) over (partition by identifier order by id) end as counterhand,
case mod(row_number() over (partition by identifier order by id), 2)
when 0 then lag(id) over (partition by identifier order by id)
else lead(id) over (partition by identifier order by id) end as counterpart
from ( select id, lr, identifier,
row_number() over (partition by identifier, lr order by id) as rn,
least( count(case when lr = 'L' then 1 end) over (partition by identifier),
count(case when lr = 'R' then 1 end) over (partition by identifier)
) as least_count
from test_data
)
where rn <= least_count
order by id -- ORDER BY is optional
;
gives you:
ID L IDEN C COUNTERPART
---------- - ---- - -----------
2 R A15C L 3
3 L A15C R 2
4 R A15C L 5
5 L A15C R 4
10 L E5A6 R 11
11 R E5A6 L 10
12 L E5A6 R 13
13 R E5A6 L 12
17 L EE5A R 18
18 R EE5A L 17
10 rows selected.
The same two case statements can be added to the second version in #mathguy's previous answer, and gives the same result.
Continuing to build on prior answer... #mathguy did the hard work, and after reading Alex's answer I think his is more compact, i'm using a union that he's not, we both used ROw_number and mod.
with cte2(ID, LR, Identifier) as (
SELECT 1,'L','B15A' FROM DUAL UNION ALL
SELECT 2,'R','A15C' FROM DUAL UNION ALL
SELECT 3,'L','A15C' FROM DUAL UNION ALL
SELECT 4,'R','A15C' FROM DUAL UNION ALL
SELECT 5,'L','A15C' FROM DUAL UNION ALL
SELECT 6,'R','D5A2' FROM DUAL UNION ALL
SELECT 9,'R','D5A2' FROM DUAL UNION ALL
SELECT 10,'L','E5A6' FROM DUAL UNION ALL
SELECT 11,'R','E5A6' FROM DUAL UNION ALL
SELECT 12,'L','E5A6' FROM DUAL UNION ALL
SELECT 13,'R','E5A6' FROM DUAL UNION ALL
SELECT 14,'R','H9S5' FROM DUAL UNION ALL
SELECT 17,'L','EE5A' FROM DUAL UNION ALL
SELECT 18,'R','EE5A' FROM DUAL),
cte1 as (select id, lr, identifier
from ( select id, lr, identifier,
row_number() over (partition by identifier, lr order by id) as rn,
least( count(case when lr = 'L' then 1 end) over (partition by identifier),
count(case when lr = 'R' then 1 end) over (partition by identifier)
) as least_count
from Cte2
)
where rn <= least_count
order by id),
cte as (Select A.*, Row_number() over (order by ID) RN from cte1 A)
SELECT A.Id
, A.LR
, A.Identifier
, case when A.LR = 'R' then 'L'
when A.LR = 'L' then 'R' end as CounterHand
, B.ID as CounterPart
FROM Cte A
INNER JOIN cte B
on A.RN+1 = B.RN
WHERE mod(A.RN,2)=1
UNION
SELECT B.Id
, B.LR
, B.Identifier
, case when B.LR = 'R' then 'L'
when B.LR = 'L' then 'R' end as CounterHand
, A.ID as CounterPart
FROM cte A
INNER JOIN cte B
on A.RN = B.RN-1
WHERE mod(B.RN,2)=0

Build dynamic query in oracle

Could you help me building a query as below?
TABLE_A
id brand color size model yn_buy
1 A blue M A -
2 A grey X C -
3 B red X B -
4 C blue S C -
TABLE_B
brand critery
A color=grey and size=X
B color=red
C size=M
I want to build a query to update table A and the answer should be:
TABLE_A
id brand color size model yn_buy
1 A blue M A N
2 A grey X C Y
3 B red X B Y
4 C blue S C N
As you can see the data on the column "critery" should be the decisor to buy or not
I'd like to use a single merge, like the following
merge into TABLE_A a
using
(
select id, brand, CASE WHEN critery THEN 'Y' ELSE 'N' END yn_buy
TABLE_A a
left join TABLE_B b ON a.brand = b.brand
) b
ON (a.id = b.id)
WHEN MATCHED THEN UPDATE set a.yn_buy = b.yn_buy
Is it possible to do something like this? maybe using execute immediate, some kind of bind...?
thank you
If I don't miss something in your particular case you don't need dynamic SQL - you can simply change TABLE_B structure and use static MERGE (because I don't know how complex your criteria can be this is just an illustration):
SQL> create table table_a (id, brand, color, size#, model#, tn_buy) as
2 select 1, 'A', 'blue', 'M', 'A', cast(null as varchar2(3)) from dual union all
3 select 2, 'A', 'grey', 'X', 'C', cast(null as varchar2(3)) from dual union all
4 select 3, 'B', 'red', 'X', 'B', cast(null as varchar2(3)) from dual union all
5 select 4, 'C', 'blue', 'S', 'C', cast(null as varchar2(3)) from dual
6 /
Table screated.
SQL> create table TABLE_B (brand, color, size#)
2 as
3 select 'A', 'grey', 'X' from dual union all
4 select 'B', 'red', null from dual union all
5 select 'C', null, 'M' from dual
6 /
Table created.
SQL> merge into TABLE_A a USING (
2 select a.id, a.brand, CASE
3 WHEN a.color = nvl(a.color, a.color) and a.size# = nvl(b.size#,a.size#)
4 THEN 'Y' ELSE 'N' END yn_buy FROM
5 TABLE_A a
6 left join TABLE_B b ON a.brand = b.brand
7 ) b
8 ON (a.id = b.id)
9 WHEN MATCHED THEN UPDATE set a.yn_buy = b.yn_buy
10 /
4 rows merged.
SQL> select * from table_a order by id;
ID B COLO S M YN_
---------- - ---- - - ---
1 A blue M A N
2 A grey X C Y
3 B red X B Y
4 C blue S C N
But of your criteria are difficult enough to be implemented using simple static condition, then you can use dynamic SQL:
SQL> create table TABLE_B1 (brand, criteria)
2 as
3 select 'A', q'[color='grey' and size# in ('X','M')]' from dual union all
4 select 'B', q'[color = 'red']' from dual union all
5 select 'C', q'[size#='M']' from dual
6 /
Table created.
SQL> update table_a set yn_buy = null;
4 rows updated.
SQL> commit;
Committed.
SQL> begin
2 for cur in (select brand, criteria from table_b1) loop
3 execute immediate
4 'update table_a set yn_buy = case when '||cur.criteria||
5 q'[ then 'Y' else 'N' end where brand = :1]' using cur.brand;
6 end loop;
7 end;
8 /
PL/SQL procedure completed.
SQL> select * from table_a;
ID B COLO S M YN_
---------- - ---- - - ---
1 A blue M A N
2 A grey X C Y
3 B red X B Y
4 C blue S C N

Select Top (Max) Amount From Two Of Four Fields (Columns)

I have this query
SELECT code, username, week1money, week2money, week3money, week4money FROM(
--subquery goes here
)
How to select the top two weeks, i.e. weeks with the highest value? I want to sum the top two weeks to be precise.
If I understand correct you want to get 2 top values per every (code, username) row and (code, username) is a key of recordset.
Supposing you can have two top weeks with the same values and you don't have nulls this might be one of solutions:
SQL> with t (id, code, week1, week2, week3, week4)
2 as (
3 select 1, 'a', 10, 15, 11, 8 from dual union all
4 select 2, 'b', 7, 4, 2, 9 from dual union all
5 select 3, 'c', 3, 3, 1, 0 from dual
6 )
7 select id, code, max(week) first_top, min(week) next_top from (
8 select id, code, row_number() over(partition by id, code order by week desc) rnk, week
9 from (
10 select t.id, t.code,
11 decode(r.rn,1,week1,2,week2,3,week3,4,week4) week
12 from t,
13 (select rownum rn from dual connect by level <= 4) r
14 ))
15 where rnk in (1,2)
16 group by id, code
17 /
ID C FIRST_TOP NEXT_TOP
---------- - ---------- ----------
3 c 3 3
1 a 15 11
2 b 9 7
If you have non-null and different values in weeks you can use something like:
SQL> with t (id, code, week1, week2, week3, week4)
2 as (
3 select 1, 'a', 10, 15, 11, 8 from dual union all
4 select 2, 'b', 7, 4, 2, 9 from dual union all
5 select 3, 'c', 3, 2, 1, 0 from dual
6 )
7 select id, code
8 , greatest(week1, week2, week3, week4) first_top
9 , greatest(
10 case when week1 < greatest(week1, week2, week3, week4) then week1 else -1e28 end,
11 case when week2 < greatest(week1, week2, week3, week4) then week2 else -1e28 end,
12 case when week3 < greatest(week1, week2, week3, week4) then week3 else -1e28 end,
13 case when week4 < greatest(week1, week2, week3, week4) then week4 else -1e28 end
14 ) second_top
15 from t
16 /
ID C FIRST_TOP SECOND_TOP
---------- - ---------- ----------
1 a 15 11
2 b 9 7
3 c 3 2
But to get the right solution more details are required.
Answering my question...
select * from(
select * from(
select week1money col from dual
union
select week2money col from dual
union
select week3money col from dual
union
select week4money col from dual
) order by col desc
) where rownum < 3
Using GREATESTS() also may help.

Resources