PLSql stored procedure to convert data of column in select statement - oracle

I want to create a pl/sql procedure in which I want to retrieve the value of column from a table based on another column and I want the output as select query having the field values of the column as the column name.
Suppose I have a table having two columns as
test1:
column 1 column 2
-----------------
abc 123
abc 234
abc 453
abab 4546
abab 454665
abab 4564566
I want the result as select 123, 234, 453 from abc when I provide the input variable for column 1

You can use the below query in procedure to retreive the result:
select listagg(column2, ',') within group (order by column1) from test1
where column1 = 'abc';

Related

Inserting selected data from table in one schema into another table in a different schema

I have a table A in Schema1 and table B in Schema2.
The tables have different columns.
Table A:
ID1 Name Code
-------------------------------
1 Skyler A0
2 Amanda A1
3 Rachel B0
4 Harvey C0
5 Louis B1
Table B:
ID Names Enterprise Modified_Date
------------------------------------------------------
1 Amanda 1 2018.08.10
2 Skyler 1 2018.08.11
As depicted, Schema1.A.Name = Schema2.B.Names
I want to insert the values "Rachel,Harvey and Louis" from A.Name into B.Names.
For b.ID, i have a sequence in place. Enterprise column is always 1 and modified date can e sysdate.
How can i achieve this in PL/SQL?
use insert Statement with select statement
insert into tabB (names,Enterprise,Modified_Date )
select Name,1,sysdate from tabA where Name in ('Rachel','Harvey','Louis');
You can do this by using below query.
insert into tableB (names,Enterprise,Modified_Date )
select Name,1,sysdate from tableA where Name not in (select distinct(Name) from tableB);

Need to get the max value in a given string after matching with a column

Consider this string '123456789' and the table consisting of col1 with the values as discribed below:
Col1
123
456
789
I need to write a query so the query will have to check for every value in the col1 and output the max matched with the string '123456789' in this i need to get output as 789.
Setup:
create table t (c varchar2(10));
insert into t values ('123');
insert into t values ('456');
insert into t values ('789');
commit;
Query:
select c, instr('123456789',c) as pos
from t
order by 2 desc
fetch first 1 row only;
Result:
C POS
---------- ----------
789 7

How to read field with comma through Oracle external table

I have a position separated text file which I have to read through Oracle external tables. In that position separated file I field as name separated by comma. For example:
123 abc,def 456. So I have to insert data into 3 columns. Fisrt column would have 123, second column would have abc,def and third column would have 456. In access parameter I have given "records delimited by newline". But while selecting data from external table, it only gives data before comma (abc). But I want to read abc,def. Can anybody help me with this?
The following works for me;
CREATE TABLE test
(
col_1 NUMBER,
col_2 VARCHAR2(30),
col_3 NUMBER
)
ORGANIZATION EXTERNAL
( TYPE ORACLE_LOADER
DEFAULT DIRECTORY MY_DIR
ACCESS PARAMETERS
( RECORDS DELIMITED BY '\r\n'
FIELDS TERMINATED BY ' '
OPTIONALLY ENCLOSED BY '"'
MISSING FIELD VALUES ARE NULL
( col_1, col_2, col_3
)
)
LOCATION (MY_DIR:'test.txt')
)
REJECT LIMIT UNLIMITED;
Bear in mind that "records delimited by '/r/n'" may be specific to Windows but my results are as below;
SQL> select * from test
2 /
COL_1 COL_2 COL_3
---------- ------------------------------ ----------
123 ABC,DEF 123
789 ABCD,EF 123
456 A,B,C,DEF 123

Convert rows to columns oracle SQL

I did not find any suitable previous answers hence posting the question. I need to convert rows to columns. The PIVOT examples all convert the rows to a single column whereas my requirement is multiple columns. My table looks like this
Type ID
test1 10
test1 20
test1 30
test2 10
test2 40
I would like it to be
Type ID Type ID
test1 10 test2 10
test1 20 test2 40
test1 30
Appreciate your suggestions/inputs!
You could enumerate rows with row_number() and make pivot:
SQLFiddle demo
select *
from (
select d.*, row_number() over(partition by type order by id) rn from data d)
pivot (max(type) type, max(id) id for type in ('test1' t1, 'test2' t2))
If the 'ID' column is the primarykey you can only have one column as primarykey in the table.

Join same table to display rows as colomn in oracle

I have a scenario where I need to take few rows from column and make it as separate column.
My present table:
Id Description
1 abc
2 abc
3 abc
4 abc
1 xyz
2 xyz
3 xyz
4 xyz
Required output:
id Desp1 Desp2
1 abc xyz
2 abc xyz
3 abc xyz
4 abc xyz
Can any one help me with this.
You could make use of the listagg function and a combination of instr and substr functions, instead of a self Join.
select id,substr(Description, 0, instr(Description, ',',1,1)-1) Desp1,
substr(Description, instr(Description, ',',1,1)+1) Desp2
from
(select id, listagg(Description,',') within group (order by Description) as
Description from sam group by id)
Note: The above query delimits the Description field by comma, and splits only into two columns as depicted in your example.

Resources