[英]extract substring using pl sql
I have a table T1 with below data 我有一个包含以下数据的表T1
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. 检查它是否是手动的,从Ns_NAME取到最后的“ _”,并检查是否重复。 In this case there is 1 duplicate.
在这种情况下,有1个重复项。 Obtain average stat [(2+3)/2] of those two rows and pump into another table T2.
获得这两行的平均状态[(2 + 3)/ 2],然后泵入另一个表T2。
Output: 输出:
T2 T2
AF_rtf_Nd Manual 2.5
I tried using substr function and used etract . 我尝试使用substr函数并使用etract。 But it is not fetching the correct result.
但这并没有获得正确的结果。
In order to look at 'Manual' records only, use a WHERE
clause. 为了仅查看“手动”记录,请使用
WHERE
子句。 Then aggregate over the substring. 然后在子字符串上聚合。 You get the substring with
INSTR
plus SUBSTR
or with REGEX_REPLACE
. 您可以使用
INSTR
加SUBSTR
或REGEX_REPLACE
获得子字符串。 Then only keep duplicates by using HAVING COUNT(*) > 1
. 然后仅使用
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 与具有
INSTR
和SUBSTR
的REGEXP_REPLACE
等效为
substr(ns_name, 1, instr(ns_name, '_', -1) - 1)
(Only difference is when there is no '_' in the string at all.) (唯一的区别是字符串中根本没有“ _”。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.