I have a table T1 with below data
Sno Ns_NAME Mode stat 1 AF_rtf_Nd_1 Manual 2 2 AF_rtf_Nd_2 Manual 3 3 AF_rtf_Nd_2i Manual 2 4 AF_rtf_Nd_3 Auto 2 5 AF_rtf_Nd_3i Auto 3
I need to perform below,
check if it is manual, fetch from Ns_NAME upto last "_" and check for duplicates. In this case there is 1 duplicate. Obtain average stat [(2+3)/2] of those two rows and pump into another table T2.
Output:
T2
AF_rtf_Nd Manual 2.5
I tried using substr function and used etract . But it is not fetching the correct result.
In order to look at 'Manual' records only, use a WHERE
clause. Then aggregate over the substring. You get the substring with INSTR
plus SUBSTR
or with REGEX_REPLACE
. Then only keep duplicates by using HAVING COUNT(*) > 1
.
insert into t2
select
min(sno),
regexp_replace(ns_name, '_[^_]*$', ''),
'Manual',
avg(stat)
from t1
where mode = 'Manual'
group by regexp_replace(ns_name, '_[^_]*$', '')
having count(*) > 1;
The equivalent to the REGEXP_REPLACE
with INSTR
and SUBSTR
is
substr(ns_name, 1, instr(ns_name, '_', -1) - 1)
(Only difference is when there is no '_' in the string at all.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.