简体   繁体   English

返回每行SQL中记录的数量

[英]Return count of records in each row SQL

I have one table like this. 我有一张这样的桌子。

 SQL> SELECT * FROM FRUIT;

 F_NAME
 ----------
 APPLE
 PPPALEP
 APLEE
 PPAALLEEPP
 ornPpfpP    
 PPdhppPP  

Above one is my source table and I want to below output.If i am giving 'P' in multiform like including capital and small both. 上面的是我的源表,我想下面的输出。如果我以多种形式给出'P',例如包括大写字母和小写字母。 I want to count only 'P' from each row. 我只想从每一行中算出“ P”。

OUTPUT
------
F_NAME      COUNT
------      -----
APPLE         2
PPPALEP       4
APLEE         1
PPAALLEEPP    4
ornPpfpP      4
PPdhppPP      6

Thanks in advance. 提前致谢。

Oracle has the very convenient regexp_count() . Oracle具有非常方便的regexp_count() So: 所以:

select f_name, regexp_count(f_name, 'P') as cnt
from fruit;

You can count the number of occurrences by replacing P with blanks and subtracting the length of the replaced string from the original string. 您可以通过用空格替换P并从原始字符串中减去替换字符串的长度来计算出现的次数。

select f_name,length(f_name)-length(replace(f_name,'P','')) cnt
from fruit

Edit: Per OP's comment, to count both P and p , use upper or lower when replacing the character with an empty string. 编辑:根据OP的注释,要同时计算Pp ,请在使用空字符串替换字符时使用upperlower

select f_name,length(f_name)-length(replace(upper(f_name),'P','')) cnt
from fruit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM