ORACLE SQL | Modifying data in ORDER BY - oracle

following structure in a ORACLE table:
FILE_NAME
-----------
12345_l.tif
12345_m.tif
12345_r.tif
12345_x.tif
12345_y.tif
Need the following result:
First *_m*
Then *_l*
Then *_r*
Then * (everything else)
Trying with:
SELECT FILE_NAME FROM TABLE
WHERE FILE_NAME LIKE '12345%'
ORDER BY regexp_replace(FILE_NAME, '_m', '_1'),
regexp_replace(FILE_NAME, '_l', '_2'),
regexp_replace(FILE_NAME, '_r', '_3')
But this gives me a wrong result.
Anybody with a hint?
TIA Matt

Change your ORDER BY to order it by a numeric:
ORDER BY regexp_replace(FILE_NAME, '_m', 1),
regexp_replace(FILE_NAME, '_l', 2),
regexp_replace(FILE_NAME, '_r', 3);
e.g.
WITH t
AS (SELECT '12345_l.tif' AS file_name FROM dual
UNION
SELECT '12345_m.tif' FROM dual
UNION
SELECT '12345_r.tif' FROM dual
UNION
SELECT '12345_x.tif' FROM dual
UNION
SELECT '12345_y.tif' FROM dual)
SELECT file_name
FROM t
ORDER BY regexp_replace(FILE_NAME, '_m', 1),
regexp_replace(FILE_NAME, '_l', 2),
regexp_replace(FILE_NAME, '_r', 3);
Gives:
==============
12345_m.tif
12345_l.tif
12345_r.tif
12345_x.tif
12345_y.tif
Hope it helps...
Alternatively you could use:
ORDER BY (CASE SUBSTR(file_name, INSTR(file_name, '_')+1, 1)
WHEN 'm' THEN 1
WHEN 'l' THEN 2
WHEN 'r' THEN 3
ELSE 4
END) ASC;
E.G.:
WITH t
AS (SELECT '12345_l.tif' AS file_name FROM dual
UNION
SELECT '12345_y.tif' FROM dual
UNION
SELECT '12345_r.tif' FROM dual
UNION
SELECT '12345_x.tif' FROM dual
UNION
SELECT '12345_m.tif' FROM dual)
SELECT file_name
FROM t
ORDER BY (CASE SUBSTR(file_name, INSTR(file_name, '_')+1, 1)
WHEN 'm' THEN 1
WHEN 'l' THEN 2
WHEN 'r' THEN 3
ELSE 4
END) ASC;
Gives:
12345_m.tif
12345_l.tif
12345_r.tif
12345_x.tif
12345_y.tif

Related

REGEXP_SUBSTR not able to process only current row

(SELECT LISTAGG(EVENT_DESC, ',') WITHIN GROUP (ORDER BY EVENT_DESC) FROM EVENT_REF WHERE EVENT_ID IN
( SELECT REGEXP_SUBSTR(AFTER_VALUE,'[^,]+', 1, level) FROM DUAL
CONNECT BY REGEXP_SUBSTR(AFTER_VALUE, '[^,]+', 1, level) IS NOT NULL
)
)
A table from which I am fetching AFTER_VALUE has values of integer which is comma seperated like
AFTER_VALUE data
Expected output
1
Event1
1,2
Event1,Event2
1,12,2,5
Event1,Event12,Event2,Event5
15,13
Event15,Event13
these are Ids in EVENT_REF table which have some description. I am trying to basically present
ex. 1,2 as Event1, Event2 and send back from query. There are multiple events so using REPLACE would be very tedious.
When using above query I'm getting error as “ORA-01722: invalid number” whenever there is more than one value in AFTER_VALUE column Ex. if there exists only one id , then the query works but for values like 1,2 or 1,13 etc it throws invalid number error.
PS: The event names are not Event1,Event2 etc , I have just put for reference.
You don't even need regular expressions for this assignment. Standard string function replace() can do the same thing, and faster. You only need an extra 'Event' at the beginning of the string, since that one doesn't "replace" anything.
Like this: (note that you don't need the with clause; I included it only for quick testing)
with
event_ref (after_value) as (
select '1' from dual union all
select '1,2' from dual union all
select '1,12,2,5' from dual union all
select '15,13' from dual
)
select after_value,
'Event' || replace(after_value, ',', ',Event') as desired_output
from event_ref
;
AFTER_VALUE DESIRED_OUTPUT
----------- -----------------------------
1 Event1
1,2 Event1,Event2
1,12,2,5 Event1,Event12,Event2,Event5
15,13 Event15,Event13
Ah,ok, looks, like you have other characters in your comma-separated list, so you can use this query:
with EVENT_REF(EVENT_ID,EVENT_DESC) as (
select 1, 'Desc 1' from dual union all
select 2, 'Desc 2' from dual union all
select 3, 'Desc 3' from dual union all
select 4, 'Desc 4' from dual union all
select 5, 'Desc 5' from dual union all
select 12, 'Desc12' from dual union all
select 13, 'Desc13' from dual union all
select 15, 'Desc15' from dual
)
select
(SELECT LISTAGG(EVENT_DESC, ',')
WITHIN GROUP (ORDER BY EVENT_DESC)
FROM EVENT_REF
WHERE EVENT_ID IN
( SELECT to_number(REGEXP_SUBSTR(AFTER_VALUE,'\d+', 1, level))
FROM DUAL
CONNECT BY level<=REGEXP_COUNT(AFTER_VALUE, '\d+')
)
)
from (
select '1' AFTER_VALUE from dual union all
select '1,2' AFTER_VALUE from dual union all
select '1,12,2,5' AFTER_VALUE from dual union all
select '15,13' AFTER_VALUE from dual
);
PS. And do not forget that to_number has 'default on conversion error' now: https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/TO_NUMBER.html
There is no need to split and concatenate substrings, just use regexp_replace:
with EVENT_REF (AFTER_VALUE) as (
select '1' from dual union all
select '1,2' from dual union all
select '1,12,2,5' from dual union all
select '15,13' from dual
)
select regexp_replace(AFTER_VALUE,'(\d+)','Event\1') from EVENT_REF;
REGEXP_REPLACE(AFTER_VALUE,'(\D+)','EVENT\1')
-----------------------------------------------
Event1
Event1,Event2
Event1,Event12,Event2,Event5
Event15,Event13

Oracle Pivot Column in group query

I have the following working query:
WITH pivot_data AS (
select PSGROUP,
PSCOLUMN as PSCOLUMN
FROM LOG_PS_STATUS
)
SELECT *
FROM pivot_data
PIVOT (
MAX(NULL) --<-- pivot_clause
FOR PSCOLUMN--<-- pivot_for_clause
IN (&PS_COLUMNS.) --<-- pivot_in_clause
);
It shows results as expected:
Values:
PSGroup PSColumn
A 1
A 2
A 3
B 1
B 2
B 3
C 3
Result is giving like:
PSGroup(Column vertically) PSColoumn(Horizontally)
1 2 3
A
B
C
Now I want to make PSGroup column as group of PSColumn and output should be like:
A
1 2 3
B
1 2 3
C
3
You can use listagg:
WITH pivot_data(PSGroup,PSColumn) AS (
select 'A', 1 FROM dual UNION all
select 'A', 2 FROM dual UNION all
select 'A', 3 FROM dual UNION all
select 'B', 1 FROM dual UNION all
select 'B', 2 FROM dual UNION all
select 'B', 3 FROM dual UNION all
select 'C', 3 FROM DUAL),
res(PSGroup,PSColumns) as(
SELECT PSGroup, LISTAGG(PSColumn, ' ') WITHIN GROUP (order by PSColumn)
FROM pivot_data
GROUP BY PSGroup)
select DECODE(PSColumns,NULL,PSGroup,NULL) AS PSGroup, PSColumns from(
select PSGroup, NULL as PSColumns from res
union all
select PSGroup, PSColumns from res)t
ORDER BY t.PSGroup, t.PSColumns NULLS first
Also note that LISTAGG(PSColumn, ' ') WITHIN GROUP (order by PSColumn) is limited to 4000 characters

splitting column in many rows gives too many rows

I am trying this statement
with value_table as
(select 1 id, '1/2/3' objnr from dual
union all
select 2, '4/5/6' from dual),
test as
(select id, objnr col from value_table where id in (1, 2))
select id, regexp_substr(col, '[^/]+', 1, level) result
from test
connect by level <= length(regexp_replace(col, '[^/]+')) + 1
order by 1
I want to get 6 rows
1 1
1 2
1 3
2 4
2 5
2 6
but I am getting the rows multiple times.
When I try it with just one id, it works without a problem.
As a workaround, I just made a loop for every single id, another solution is to use distinct, but that takes ages to execute, when I try it, with real data(over 1000 entries).
Can someone provide a more sophisticated solution?
This query is executed
with test as
(
select 1 id, '1/2/3' objnr from dual union all
select 2 id, '4/5/6' objnr from dual
)
select id, regexp_substr (objnr, '[^/]+', 1, rn) result
from test
cross
join (select rownum rn
from (select max (length (regexp_replace (objnr, '[^/]+'))) + 1 mx
from test
)
connect by level <= mx
)
where regexp_substr (objnr, '[^/]+', 1, rn) is not null
and id in (1, 2)
order by id,result ;
ID RESULT
1 1
1 2
1 3
2 4
2 5
2 6
If you use Oracle 11g, you can also use REGEXP_COUNT instead of the combination of REGEXP_REPLACE and LENGTH, which would look like this:
cross
join (select rownum rn
from (select max (regexp_count (objnr, '/') + 1) mx
from test
)
connect by level <= mx
)
to have outer join behaviour so made a slight variation as below:
with test as
(
select 1 id, '1/2/3' objnr from dual union all
select 2 id, '4/5/6' objnr from dual
)
select id, regexp_substr (objnr, '[^/]+', 1, rn) result
from test
left outer join (select rownum rn
from (select max (regexp_count (objnr, '/') + 1) mx
from test
)
connect by level <= mx
) splits
ON splits.rn <= length(regexp_replace (objnr, '[^/]+'))+1
and id in (1, 2)
order by id,result ;
try distinct as used below
with value_table as
(select 1 id, '1/2/3' objnr from dual
union all
select 2, '4/5/6' from dual),
test as
(select id, objnr col from value_table where id in (1, 2))
select distinct id, regexp_substr(col, '[^/]+', 1, level) result
from test
connect by level <= regexp_count(col, '[^/]+')
order by 1
tried with sqlfiddle http://sqlfiddle.com/#!4/03d80/14
EDIT:
If you don't want distinct try the below
with value_table as
(select 1 id, '1/2/3' objnr from dual
union all
select 2, '4/5/6' from dual),
test as
(select id, objnr col from value_table where id in (1, 2))
select id, regexp_substr(col, '[^/]+', 1, level) result
from test
connect by level <= regexp_count(col, '[^/]+')
and id = prior id
and prior dbms_random.value is not null
order by 1
which uses the model clause PRIOR the sqlfiddle link is http://sqlfiddle.com/#!4/03d80/31

Group 'n' rows in to columns - oracle

I have a situation where I need to split 'n' rows in to column group. For example, Below is dataset
COMMENT_TEXT
T1
T2
T3
T4
T5
T6
Expected Output:
SUN MON TUE
T1 T2 T3
T4 T5 T6
My Query:
SELECT htbp1.comment_text
FROM hxc_time_building_blocks htbp,
hxc_time_building_blocks htbp1
WHERE htbp1.parent_building_block_id = htbp.time_building_block_id
AND htbp1.parent_building_block_ovn = htbp.parent_building_block_ovn
AND htbp.parent_building_block_id = 116166
AND htbp.parent_building_block_ovn = 1
ORDER BY htbp1.time_building_block_id
Is there any way I can do PIVOT with a 'n' rows and without aggregate function?
Edit: T1/T2/T3 as sample data sets but in real it can be any random free text or null.
SELECT * FROM (SELECT htbp1.comment_text, TO_CHAR (htbp.start_time, 'DY') par_time,
trunc((rownum-1) / 7) buck
FROM hxc_time_building_blocks htbp,
hxc_time_building_blocks htbp1,
hxc_timecard_summary hts
WHERE hts.RESOURCE_ID = :p_resource_id
AND TRUNC(hts.STOP_TIME) = TRUNC(:p_wkend_date)
AND htbp1.parent_building_block_id = htbp.time_building_block_id
AND htbp1.parent_building_block_ovn = htbp.parent_building_block_ovn
AND htbp.parent_building_block_id = hts.timecard_id
AND htbp.parent_building_block_ovn = hts.timecard_ovn
ORDER BY htbp1.time_building_block_id ) PIVOT( max(comment_text) FOR par_time
IN ('SUN' AS "SUN",
'MON' AS "MON",
'TUE' AS "TUE",
'WED' AS "WED",
'THU' AS "THU",
'FRI' AS "FRI",
'SAT' AS "SAT"));
When I added the another table 'hxc_timecard_summary' which is parent then data is going crazy, but if I use the hardcoded parameters like the one in the first then the rows are showing up fine.
PIVOT also uses an aggregate function but you don't need a GROUP BY:
with tab as (
select sysdate - 7 date_col, 'T1' comment_text from dual
union all select sysdate - 6, 'T2' from dual
union all select sysdate - 5, 'T3' from dual
union all select sysdate - 4, 'T4' from dual
union all select sysdate - 3, 'T5' from dual
union all select sysdate - 2, 'T6' from dual
union all select sysdate - 1, 'T7' from dual
)
select * from (select to_char(date_col, 'D') day_of_week, comment_text from tab)
PIVOT (max(comment_text) for day_of_week in (7 as sun, 1 as mon, 2 as tue));
Also, I suppose you need the second column with a date to form your new columns.
And you cannot use expressions for FOR clause - this should be column(s). For example, this won't work:
select * from tab
PIVOT (max(comment_text) for to_char(date_col, 'D') in (7 as sun, 1 as mon, 2 as tue));
because of to_char(date_col, 'D')
Try using pivot.
It allows rows to be mapped to columns.
Its from 11g onwards I believe.
with tab as (
select 'T1' comment_text from dual
union all select 'T2' from dual
union all select 'T3' from dual
union all select 'T4' from dual
union all select 'T5' from dual
union all select 'T6' from dual
)
select regexp_substr(txt, '[^,]+', 1, 1) sun,
regexp_substr(txt, '[^,]+', 1, 2) mon,
regexp_substr(txt, '[^,]+', 1, 3) tue
from (
select buck, wm_concat(comment_text) txt
from (
select comment_text, trunc((rownum-1) / 3) buck
from (select comment_text from tab order by comment_text)
)
group by buck
);
wm_concat(comment_text) (Oracle 10g) =
listagg(comment_text, ',') within group(order by comment_text) (Oracle 11g)
But these two functions are both aggregate
My third try, no aggregate functions at all (works fine in Oracle 10g)
with tab as (
select 'T1' comment_text from dual
union all select 'T2' from dual
union all select 'T3' from dual
union all select 'T4' from dual
union all select 'T5' from dual
union all select 'T6' from dual
)
select regexp_substr(txt, '[^(#####)]+', 1, 1) sun,
regexp_substr(txt, '[^(#####)]+', 1, 2) mon,
regexp_substr(txt, '[^(#####)]+', 1, 3) tue
from (
select sys_connect_by_path(comment_text, '#####') txt, parent_id
from (
select rownum id, comment_text, mod(rownum-1, 3) parent_id
from (select comment_text from tab order by comment_text)
)
start with parent_id = 0
connect by prior id = parent_id
) where parent_id = 2;

Oracle/Hibernate Like: Finding String that only contain a single ';'

I'm trying to find all the records that contains only a single ; in a column.
For example
a;sdasd
as;dasd;dasd
as;dasd;das
only a;sdasd will be returned.
I have tried %;% but it will return all the strings that contain ;.
SELECT id
FROM your_table
WHERE LENGTH(col) - LENGTH(REPLACE(col,';')) = 1;
Is one solution.
Another uses REGEXP_LIKE:
WITH q AS (SELECT 1 ID, 'a;b;c;' str FROM dual
UNION
SELECT 2, ';abc' FROM dual
UNION
SELECT 3, 'a;b;c;defg;h' FROM dual
UNION
SELECT 4, 'abcdefghi;' FROM dual
UNION
SELECT 5, 'ab;cde' FROM dual
UNION
SELECT 6, 'abcdef' FROM dual)
SELECT *
FROM q
WHERE regexp_like(str,'^[^;]*;[^;]*$');
ID STR
---------- ------------
2 ;abc
4 abcdefghi;
5 ab;cde
Ine other possibility (no String manipulation is necessary):
FROM your_table
WHERE col like '%;%' AND INSTR(col, ';', 1) = INSTR(col, ';', -1)

Resources