Quantcast
Channel: Teradata Forums - Database
Viewing all articles
Browse latest Browse all 14773

Search for first substring in string with condition - response (2) by ulrich

$
0
0

performance depend on your data volumes but at least this does the trick with your data ;-)
I used the volatile table to make it a bit more clearer 
First splitt your data into chars and keep only  the 'a' and 'b'

create volatile table t2 as
(
select seq,
       str,
       id,
       substr(str,id,1) as v
from t1
     cross join (select current_date - calendar_date as id from sys_calendar.calendar where id between 1 and 100) i
where 
     id <= characters(str)
     and v in ('a','b')
) with data 
primary index (seq)
on commit preserve rows
;

the check for 'aba' with olap or keep the b if it is the fist
and finally aggregate everything to keep only the first.

select seq, str, min(id)
from 
(
	select seq, 
	       str, 
	       id, 
	       v
	from t2
	qualify 
	         (min(v) over (partition by seq order by id rows between 1 preceding and 1 preceding) 
	         || v  
	         || min(v) over (partition by seq order by id rows between 1 following and 1 following) <> 'aba'
	        
	         and v = 'b'
	         ) or (
	          v = 'b' and id = min(id) over (partition by seq)
	         )
) as t
group by 1,2;

a bit of bruce force but as mentioned it does the trick


Viewing all articles
Browse latest Browse all 14773

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>