Columns to row converting - sql

Table_A:
AA
BB
CC
DD
Output:
AA,BB,CC,DD
Any one help this

LISTAGG seems to be one choice:
Sample data:
SQL> with table_a(col) as
2 (select 'AA' from dual union all
3 select 'BB' from dual union all
4 select 'CC' from dual union all
5 select 'DD' from dual
6 )
Query:
7 select listagg(col, ',') within group (order by col) result
8 from table_a;
RESULT
------------------------------
AA,BB,CC,DD
SQL>

informatica only solution -
SRT : sort the data by col1.
EXP : create 3 ports. make sure their length be >3000 char
in_col1 = col1
v_col1 = col1|v_col1
p_col1= col1
o_col1= v_col1
AGG: create 2 ports
in_col1 <-- link o_col1 from previous step2
o_col1 = MAX(in_col1) -- link this to final target
Normally, there should be a key column by which you want to aggregate the strings. if you have any, you need to change step2 in v_col1.

Related

how to get the number after '-' in Oracle

I have some strings in my table. They are like 1101-1, 1101-2, 1101-10, 1101-11 pulse, shock, abc, 1104-2, 1104-11, 2201-1, 2202-4. I tried to sort them like below:
1101-1
1101-2
1101-10
1101-11
1104-2
1104-11
2201-1
2202-4
abc
pulse
shock
But I can't get the sort correctly. Below is my codes:
select column from table
order by regexp_substr(column, '^\D*') nulls first,
to_number(substr(regexp_substr(column, '\d+'),1,4)) asc
Sort numbers as numbers:
first the ones in front of the hyphen (line #16)
then the ones after it (line #17),
then the rest (line #18)
Mind the to_number function! Without it, you'll be sorting strings! and get the wrong result.
SQL> with test (col) as
2 ( select '1101-1' from dual union all
3 select '1101-2' from dual union all
4 select '1101-10' from dual union all
5 select '1101-11' from dual union all
6 select 'pulse' from dual union all
7 select 'shock' from dual union all
8 select 'abc' from dual union all
9 select '1104-2' from dual union all
10 select '1104-11' from dual union all
11 select '2201-1' from dual union all
12 select '2202-4' from dual
13 )
14 select col
15 from test
16 order by to_number(regexp_substr(col, '^\d+')),
17 to_number(regexp_substr(col, '\d+$')),
18 col;
COL
-------
1101-1
1101-2
1101-10
1101-11
1104-2
1104-11
2201-1
2202-4
abc
pulse
shock
11 rows selected.
SQL>
For your examples, this should do:
order by regexp_substr(column, '^[^-]+'), -- everything before the hyphen
len(column),
column
To get the number after '-' specifically:
with ttt (col) as (
select cast(column_value as varchar2(10)) as second_str
from table(sys.dbms_debug_vc2coll
( '1101-1'
, '1101-2'
, '1101-10'
, '1101-11'
, '1104-2'
, '1104-11'
, '2201-1'
, '2202-4'
, 'abc'
, 'pulse'
, 'shock'
))
)
select col
, regexp_substr(col, '(^\d+-)(\d+)', 1, 1, '', 2)
from ttt;
COL SECOND_STR
---------- ----------
1101-1 1
2201-1 1
1101-10 10
1101-11 11
1104-11 11
1101-2 2
1104-2 2
2202-4 4
abc
pulse
shock
11 rows selected
This treats the text string as two values, (^\d+-) followed by (\d+), and takes the second substring (the final '2' parameter). As only positional parameters are allowed for built-in SQL functions, you also have to specify occurrence (1) and match param (null, as we don't care about case etc).

Display all the names of the employees whose names having letter A as 2nd occurrence without using like operator

For the above query, I am using regexp_count but in SQL command line I am getting regexp_count :invaild identifier;
select * from table_name WHERE (REGEXP_COUNT(column_name, 'A')) >2;
Is this query works?
I'm not sure what you really want; is it to return names whose 2nd letter is "a", or names that have two or more letters "a" within.
Anyway, pick the one you find appropriate.
SQL> create table test as
2 (select 'saritha' col from dual union all
3 select 'mamatha' from dual union all
4 select 'vaisnavi' from dual union all
5 select 'sai' from dual union all
6 select 'vijaya' from dual union all
7 select 'kumar' from dual
8 );
Table created.
2 or more letters "a":
SQL> select col
2 from test
3 where regexp_count(col, 'a') >= 2;
COL
--------
saritha
mamatha
vaisnavi
vijaya
2nd letter is "a":
SQL> select col
2 from test
3 where substr(col, 2, 1) = 'a';
COL
--------
saritha
mamatha
vaisnavi
sai
SQL>
On Oracle 10g, which doesn't support REGEXP_COUNT function, one option is to replace all letters a with an empty string (basically, you'd remove all letters a) and fetch rows whose difference of full column length and "replaced" column length is >= 2. Something like this:
SQL> select col
2 from test
3 where length(col) - length(replace(col, 'a', '')) >= 2;
COL
--------
saritha
mamatha
vaisnavi
vijaya
SQL>

Find strings starting with alphanumeric in Oracle

I want to search for all records starting alphabet or number only.
I know there is REGEXP_LIKE to find if col contains alphanumeric but couldn't apply it for starting with.
SELECT * FROM mytable WHERE col1 like 'ABC:XYZ%'
I have data in below format:--
ABC:XYZ
ABC:XYZ (ERW)
ABC:XYZ TMN
ABC:XYZ123
ABC:XYZRTY:YER
I am trying to get only below output
ABC:XYZ
ABC:XYZ123
ABC:XYZRTY:YER
Regards
Something like this? Sample data up to line #7; query you might be interested in begins at line #8.
SQL> with mytable (col1) as
2 (select 'ABC:XYZ' from dual union all
3 select 'ABC:XYZ (ERW)' from dual union all
4 select 'ABC:XYZ TMN' from dual union all
5 select 'ABC:XYZ123' from dual union all
6 select 'ABC:XZYRTY:YER' from dual
7 )
8 select col1
9 from mytable
10 where not regexp_like(col1, '[^[:alnum:]:]');
COL1
--------------
ABC:XYZ
ABC:XYZ123
ABC:XZYRTY:YER
SQL>

Sort a value list that contains letters and also numbers in a specific order

I have a problem in SQL Oracle, I'm trying to create a view that contains values with letters and numbers and I want to sort them in a specific order.
Here is my query:
create or replace view table1_val (val, msg_text) as
select
val, msg_text
from
table_val
where
val in ('L1','L2','L3','L4','L5','L6','L7','L8','L9','L10','L11','L12','L13','L14','G1','G2','G3','G4')
order by lpad(val, 3);
The values are displayed like this:
G1,G2,G3,G4,L1,L2,L3,L4,L5,L6,L7,L8,L9,L10,L11,L12,L13
The thing is that I want to display the L values first and then the G values like in the where condition. The 'val' column is VARCHAR2(3 CHAR). The msg_text column is irrelevant. Can someone help me with that? I use Oracle 12C.
You must interpret the second part of the val column as a number
order by
case when val like 'L%' then 0 else 1 end,
to_number(substr(val,2))
This work fine for your current data, but may fail in future if a new record is added with non-numeric structure.
More conservative (and more hard to write), but safe would be to used a decode for all the current keys, ordering unknown keys on the last position (id = 18 in the example):
order by
decode(
'L1',1,
'L2',2,
'L3',3,
'L4',4,
'L5',5,
'L6',6,
'L7',7,
'L8',8,
'L9',9,
'L10',10,
'L11',11,
'L12',12,
'L13',13,
'G1',14,
'G2',15,
'G3',16,
'G4',17,18)
You can't do anything based on the order of the WHERE condition
But you can use a CASE on the ORDER BY
ORDER BY CASE
WHEN SUBSTR(val, 1, 1) = 'L' THEN 1
WHEN SUBSTR(val, 1, 1) = 'G' THEN 2
ELSE 3
END,
TO_NUMBER (SUBSTR(val, 2, 10));
Another option to consider might be using regular expressions, such as
SQL> with table1_val (val) as
2 (select 'L1' from dual union all
3 select 'L26' from dual union all
4 select 'L3' from dual union all
5 select 'L21' from dual union all
6 select 'L11' from dual union all
7 select 'L4' from dual union all
8 select 'G88' from dual union all
9 select 'G10' from dual union all
10 select 'G2' from dual
11 )
12 select val
13 from table1_val
14 order by regexp_substr(val, '^[[:alpha:]]+') desc,
15 to_number(regexp_substr(val, '\d+$'));
VAL
---
L1
L3
L4
L11
L21
L26
G2
G10
G88
9 rows selected.
SQL>

SQL Query to show string before a dash

I would like to execute a query that will only show all the string before dash in the particular field.
For example:
Original data: AB-123
After query: AB
You can use substr:
SQL> WITH DATA AS (SELECT 'AB-123' txt FROM dual)
2 SELECT substr(txt, 1, instr(txt, '-') - 1)
3 FROM DATA;
SUBSTR(TXT,1,INSTR(TXT,'-')-1)
------------------------------
AB
or regexp_substr (10g+):
SQL> WITH DATA AS (SELECT 'AB-123' txt FROM dual)
2 SELECT regexp_substr(txt, '^[^-]*')
3 FROM DATA;
REGEXP_SUBSTR(TXT,'^[^-]*')
---------------------------
AB
You can use regexp_replace.
For example
WITH DATA AS (
SELECT 'AB-123' as text FROM dual
UNION ALL
SELECT 'ABC123' as text FROM dual
)
SELECT
regexp_replace(d.text, '-.*$', '') as result
FROM DATA d;
will lead to
WITH DATA AS (
2 SELECT 'AB-123' as text FROM dual
3 UNION ALL
4 SELECT 'ABC123' as text FROM dual
5 )
6 SELECT
7 regexp_replace(d.text, '-.*$', '') as result
8 FROM DATA d;
RESULT
------------------------------------------------------
AB
ABC123
I found this simple
SELECT distinct
regexp_replace(d.pyid, '-.*$', '') as result
FROM schema.table d;
pyID column contains ABC-123, DEF-3454
SQL Result:
ABC
DEF