how to split one string column of `(12345)some_string` to two column `12345`, `some_string` in Oracle - oracle

As the question,
how to split one string column of (12345)some_string to two-column 12345and some_string in Oracle?
Notice: Not all the columns are (12345)some_string, part of columns are only some_string without (12345), the two columns are null and some string

With sample data you posted, this could be one option (line #5 onward):
SQL> with test (col) as
2 (select '(12345)some_string' from dual union all
3 select 'another_string' from dual
4 )
5 select regexp_substr(col, '\d+') col1,
6 substr(col, instr(col, ')') + 1) col2
7 from test;
COL1 COL2
------------------ ------------------
12345 some_string
another_string
SQL>

Assuming the following table:
create table my_table (my_column varchar2(30));
insert into my_table values ('(12345)some_string');
commit;
1) Add a new column to the table
alter table my_table add new_column number;`
2) Fill the new column
update my_table set new_column = regexp_substr(my_column, '^\(([1-9]+)\)', 1, 1, NULL, 1);
3) Update the original column
update my_table set my_column = regexp_replace(my_column, '^\([1-9]+\)', '');

Related

Convert Oracle to Hive - SubQuery can contain only 1 item in Select List

Code and sample data to try on your system:
CREATE TABLE "EMP"
( "DR_SID" NUMBER,
"DR_NAME" VARCHAR2(50 BYTE) COLLATE "USING_NLS_COMP",
"ACTIVE_FLAG" VARCHAR2(1 BYTE) COLLATE "USING_NLS_COMP",
"LAST_UPDATED_TIME" TIMESTAMP (6),
"DATA_SOURCE" VARCHAR2(100 BYTE) COLLATE "USING_NLS_COMP",
"ROW_LIMIT" VARCHAR2(50 BYTE) COLLATE "USING_NLS_COMP",
"VERSION#" NUMBER,
"PARENT_DR_SID" NUMBER
)
;
REM INSERTING into EMP
SET DEFINE OFF;
Insert into EMP (DR_SID,DR_NAME,LAST_UPDATED_TIME,VERSION#,PARENT_DR_SID) values (1,'this should not come1',to_timestamp('18-APR-20 05.05.52.425734000 AM','DD-MON-RR HH.MI.SSXFF AM'),1,1);
Insert into EMP (DR_SID,DR_NAME,LAST_UPDATED_TIME,VERSION#,PARENT_DR_SID) values (2,'come',to_timestamp('19-SEP-20 07.18.56.271199000 AM','DD-MON-RR HH.MI.SSXFF AM'),1,2);
Insert into EMP (DR_SID,DR_NAME,LAST_UPDATED_TIME,VERSION#,PARENT_DR_SID) values (3,'come123',to_timestamp('13-FEB-21 05.05.51.645956000 AM','DD-MON-RR HH.MI.SSXFF AM'),1,3);
Insert into EMP (DR_SID,DR_NAME,LAST_UPDATED_TIME,VERSION#,PARENT_DR_SID) values (4,'come456',to_timestamp('13-FEB-21 05.05.51.951505000 AM','DD-MON-RR HH.MI.SSXFF AM'),1,4);
Insert into EMP (DR_SID,DR_NAME,LAST_UPDATED_TIME,VERSION#,PARENT_DR_SID) values (5,'this should not come2',to_timestamp('18-APR-20 05.05.52.425734000 AM','DD-MON-RR HH.MI.SSXFF AM'),2,1);
Insert into EMP (DR_SID,DR_NAME,LAST_UPDATED_TIME,VERSION#,PARENT_DR_SID) values (6,'this should COME',to_timestamp('18-APR-20 05.05.52.425734000 AM','DD-MON-RR HH.MI.SSXFF AM'),3,1);
SELECT DR_SID, DR_NAME, LAST_UPDATED_TIME, VERSION#, PARENT_DR_SID FROM emp ;
the below query needs to be converted into Hive, can someone help?
SELECT DR_SID, DR_NAME, LAST_UPDATED_TIME, VERSION#, PARENT_DR_SID FROM emp t
where (version#,parent_dr_sid)
in (select max(version#),parent_dr_sid from emp group by parent_dr_sid)
;
I am try to find out which record is latest, so am using version# column (if
version# column has the max value then the record is latest and its previous records are old and not to display).
Now how the records are linked with each other, so we have two columns, dr_sid is pk and parent_dr_sid contains same value to show this record is linked with which old record.
you can see the example here, in the given sample code, dr_sid = 1 is present 3 times in parent_dr_sid, all these 3 records
of parent_dr_sid have the same value as 1 (which is linked to dr_sid).
Now I want the below o/p, can you do the same in hive?
FYI - we cant update the table so trying to update the record in this way and fetching in this way.
DR_SID, DR_NAME, LAST_UPDATED_TIME, VERSION#, PARENT_DR_SID
1 this should not come1 18-APR-20 05.05.52.425734000 AM 1 1
2 come 19-SEP-20 07.18.56.271199000 AM 1 2
3 come123 13-FEB-21 05.05.51.645956000 AM 1 3
4 come456 13-FEB-21 05.05.51.951505000 AM 1 4
5 this should not come2 18-APR-20 05.05.52.425734000 AM 2 1
6 this should COME 18-APR-20 05.05.52.425734000 AM 3 1
Use left semi join to emulate in semantics for tuples.
with input as (
select inline(array(
(1,1),
(1,2),
(2,1),
(2, 2)
)) as (c1, c2)
)
, flt as (
select inline(array(
(1,1),
(2, 2)
)) as (f1, f2)
)
select *, split(version(), ' ')[0] as v
from input
left semi join flt
on input.c1 = flt.f1
and input.c2 = flt.f2
input.c1
input.c2
v
1
1
3.1.3000.7.1.7.0-551
2
2
3.1.3000.7.1.7.0-551
I don't know Hive so these might well be completely useless suggestions; however, see if it helps.
If you can use a subquery in FROM clause, you might do the following:
select e.*
from emp e join (select max(a.create_tm) create_tm, a.open_dt
from emp a group by a.open_dt
) x
on x.create_tm = e.create_tm
and x.open_dt = e.open_dt;
Or, make your subquery return a single column by concatenating values. They look like "time" and "date" (I don't know their datatypes so you might need to apply e.g. TO_CHAR function to these columns; no problem in that, as long as it returns desired result):
select *
from emp
where concat(create_tm, open_dt) in (select concat(max(create_tm), open_dt)
from emp
group by open_dt);
this is working:
SELECT DR_SID, DR_NAME, LAST_UPDATED_TIME, VERSION#--, PARENT_DR_SID
FROM emp t join
(select max(version#) v,parent_dr_sid from emp group by parent_dr_sid) t2
on t.version#=t2.v and t.parent_dr_sid = t2.parent_dr_sid
;

oracle sql update with sub select

I want to update 1 field of the last 2 rows of a table. So I need a subquery.
Both sql works - how can I combine these 2 SQL commands?
select command (works, last 2 rows):
SELECT * FROM (select * from mytable WHERE id='62741' ORDER BY lfdnr DESC) mytable2 WHERE rownum <= 2;
Result:
LFDNR ID M2
361782 62741 8,5
361774 62741 8,6
Update (?, exists, in, merge ?)
UPDATE mytable set m2='8,4' WHERE EXISTS (select * from mytable WHERE id='62741' and rownum <=2 ORDER BY lfdnr DESC);
Result:
Fehlerbericht -
SQL-Fehler: ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Thank you for helping me!
Michael
You could use rowid pseudocolumn:
update mytable set m2 = '8, 4'
where rowid in (select rowid
from (
select rowid, row_number() over (order by lfdnr desc) rn
from mytable where id = '62741')
where rn <= 2 )
Test:
create table mytable (id varchar2(5), lfdnr number(5), m2 varchar2(10));
insert into mytable values ('62705', 1, 'abc');
insert into mytable values ('62741', 2, 'xyz');
insert into mytable values ('62741', 3, 'qwe');
insert into mytable values ('62741', 4, 'rty');
ID LFDNR M2
----- ------ ----------
62705 1 abc
62741 2 xyz
62741 3 8, 4
62741 4 8, 4

Oracle Sibling Structure

I have a structure that I store equal records in a database table. You can think that these records are siblings. For example I have two records in this table; 1=2 and 1=3. And I need a query that will return all siblings of a given record. Let me give an example;
This is my table with two columns:
create table SIBLINGSTEST(col1 number, col2 number);
I have 2 records, 1=2 and 1=3
insert into SIBLINGSTEST values(1,2);
insert into SIBLINGSTEST values(1,3);
I thought using connect by is the best solution for this situation, and write the following query:
SELECT * FROM SIBLINGSTEST
START WITH (col1 = 1 or col2 = 1)
CONNECT BY NOCYCLE (
(PRIOR col1 = col1) or
(PRIOR col1 = col2) OR
(PRIOR col2 = col1) or
(PRIOR col2 = col2))
This query returns correct results, returning both rows.
If I use 2 as a parameter, the query also runs correctly, returning again both rows.
But if I use 3 as a parameter, the query does not run as I expected, returning only the start row.
SELECT * FROM SIBLINGSTEST
START WITH (col1 = 3 or col2 = 3)
CONNECT BY NOCYCLE (
(PRIOR col1 = col1) or
(PRIOR col1 = col2) OR
(PRIOR col2 = col1) or
(PRIOR col2 = col2))
I wonder why the results of 2 and 3 differs. Any help or idea will be appriciated.
Thanks.
I get both rows with your last query as expected:
SQL> SELECT * FROM SIBLINGSTEST
2 START WITH (col1 = 3 or col2 = 3)
3 CONNECT BY NOCYCLE (
4 (PRIOR col1 = col1) or
5 (PRIOR col1 = col2) OR
6 (PRIOR col2 = col1) or
7 (PRIOR col2 = col2));
COL1 COL2
---------- ----------
1 3
1 2
However I would not choose to model it this way. If what you really want is to record that 1, 2, 3 are siblings then I would use:
create table siblings_group (group_id number);
create table people (person_id number, group_id number);
insert into siblings_group values (1);
insert into people values (1, 1);
insert into people values (2, 1);
insert into people values (3, 1);
Then to find all siblings of 3:
SQL> select person_id from people where group_id =
2 (select group_id from people where person_id=3);
PERSON_ID
----------
1
2
3

How to display comma separated descriptions based on comma separated values in Oracle 10g?

I am new to Oracle technology. Earlier I posted 2 posts for the same issue due to lack of understanding the requirement.
Table 1:
MSGID
-----
1,2,3
2,3
4
null
null
Table 2:
MID MSGDESC
---- -------
1 ONE
2 TWO
3 THREE
4 FOUR
Expected output:
XCOL DESC
----- -----
1,2,3 ONE,TWO,THREE
2,3 TWO,THREE
4 FOUR
I am not able to fulfil this requirement. Please provide me one solution.
Note: tables don't have any unique or primary key values. Table 1 has 5000 records and table 2 only has 80 records with descriptions.
create table Table1 (MSGID varchar2(100));
insert into Table1 values ('1,2,3');
insert into Table1 values ('2,3');
insert into Table1 values ('4');
insert into Table1 values (null);
insert into Table1 values (null);
create table Table2 (MID varchar2(100), MSGDESC varchar2(100));
insert into Table2 values ('1','ONE');
insert into Table2 values ('2','TWO');
insert into Table2 values ('3','THREE');
insert into Table2 values ('4','FOUR');
select
msgid as xcol,
"DESC",
col1, col2, ..., col12
from
Table1
left join (
select
msgid,
wm_concat(msgdesc) as "DESC"
from
(
select
msgid,
msgdesc
from
(select distinct msgid from Table1 where ...)
cross join (
select level as occ from dual connect by level <= 100)
)
left join Table2
on mid = regexp_substr(msgid, '[^,]+', 1, occ)
where
occ <= regexp_count(msgid, ',') + 1
order by msgid, occ
)
group by msgid
) using (msgid)

single Column Update for multiple rows in oracle10g

Update table_1 set col1= 1 where col2 = 'ONE';
update table_1 set col1= 2 where col2 = 'TWO';
Update table_1 set col1= 3 where col2 = 'THREE';
...
update table_1 set col1= 100 where col2 = 'HUNDRED';
Is there any simplified way to achive this in a single query instead of writing 100 update statemnets in oracle10g??
I think there might be a solution with Oracle Case-Statement or the decode-function, although it will be a quite long statement and I am not quite sure what the advantage over 100 update statements might be. Also I am not aware of any limitations regarding length of parameter-lists, etc.
Example for Case:
update table_1
set col1 = CASE col2
WHEN 'ONE' THEN 1
WHEN 'TWO' THEN 2
WHEN 'THREE' THEN 3
WHEN 'FOUR' THEN 4
WHEN 'FIVE' THEN 5
WHEN 'SIX' THEN 6
WHEN 'SEVEN' THEN 7
WHEN 'EIGHT' THEN 8
...
WHEN 'HUNDRED' THEN 100
ELSE col2
END;
Example for decode:
update table_1
set col1 = decode(col2,
'ONE', 1,
'TWO', 2,
'THREE', 3,
'FOUR', 4,
'FIVE', 5,
'SIX', 6,
'SEVEN', 7,
'EIGHT', 8,
...
'HUNDRED', 100,
col2);
You could make use of the Julian spelling formats (search asktom.oracle.com for more details)
Here's output from my session:-
create table table_1 (col_1 number, col_2 varchar(20))
insert into table_1 (col_1, col_2) values (null, 'THIRTY-THREE')
insert into table_1 (col_1, col_2) values (null, 'SEVEN')
insert into table_1 (col_1, col_2) values (null, 'EIGHTY-FOUR')
select * from table_1
COL_1 COL_2
THIRTY-THREE
SEVEN
EIGHTY-FOUR
update /*+bypass_ujvc*/
(select t1.col_1, spelled.n
from
table_1 t1
inner join
(select n, to_char(to_date (n, 'J'),'JSP') spelled_n from
(select level n from dual connect by level <= 100)) spelled
on t1.col_2 = spelled.spelled_n
)
set col_1 = n
select * from table_1
COL_1 COL_2
33 THIRTY-THREE
7 SEVEN
84 EIGHTY-FOUR
The nasty hint (bypass_ujvc) ignores the fact that the inline view isn't key preserved - in practice you should use a merge statement instead. But this isn't a real-world scenario, right! (And you'll have to treat your 'HUNDRED' as a special case = 'ONE HUNDRED'.)

Resources