简体   繁体   English

DB Object 名称捕获以 oracle db 结尾的最后一个字符

[英]DB Object name capture the last character ending with in oracle db

I am trying to capture the first character search vs the last character and compare the overall count match search for all the Object names (tables, views, table partitions, synonyms...) from dba_objects, I had a similar issue to capture all the object names, but in this case first characters, so i have used this query我正在尝试捕获第一个字符搜索与最后一个字符,并比较来自 dba_objects 的所有 Object 名称(表、视图、表分区、同义词...)的总体计数匹配搜索,我有一个类似的问题来捕获所有object 个名字,但在这种情况下是第一个字符,所以我使用了这个查询

In order to keep simple my actual and clear question, please find below two queries为了使我的实际问题简单明了,请在下面找到两个查询

Query1 - Capture the counts of database objects BEGINNING with Query1 - 捕获数据库对象的计数 BEGINNING with

owner counts - 70678所有者计数- 70678

object_name counts - 121341 object_name 计数- 121341

object_type - 128322 object_type - 128322

SELECT
   owner AS schema_name, --70,678
   object_name, --1,21,341
   object_type,--1,28,322
   REGEXP_REPLACE(OBJECT_NAME, '^([A-Z0-9$]{1,})_.*', '\1') as BEGINNING,
   count(*),
   round(100*ratio_to_report(count(*)) over (), 4) percentage 
FROM
   dba_objects 
GROUP BY
   owner,
   object_name,
   object_type,
   REGEXP_REPLACE(OBJECT_NAME, '^([A-Z0-9$]{1,})_.*', '\1') 
ORDER BY
   percentage desc;

Results as Expected - Satisfied结果符合预期 -满意

OBJECT_NAME       BEGINNING COUNT(*) PERCENT
ABC_CUST_INFO_D   ABC       20      .00010
BBC_CUST_ENTRY_F  BBC       100     .030
FHS_PRDCT_STST_T  A$f       194     .031
GHS_INVTR_CD_DRY  A1B       493     .051

Query2 - Capture the counts of database objects ENDING with Query2 - 捕获以 ENDING 结尾的数据库对象的计数

owner counts - 71881所有者计数- 71881

object_name counts - 121341 object_name 计数- 121341

object_type - 128322 object_type - 128322

select
   owner,--71,881
   object_name,--1,21,341
   object_type,--1,28,322
   regexp_substr(object_name, '[^_]*$') ENDING,
   count(*) COUNT,
   --count(*) / sum(count(*)) over(partition by owner) ratio 
   round(100*ratio_to_report(count(*)) over (), 4) percentage 
from
   dba_objects 
group by
   owner,
   object_name,
   object_type,
   regexp_substr(object_name, '[^_]*$')
   ORDER BY
   percentage desc;

Results as Expected - Satisfied结果符合预期 -满意

OBJECT_NAME       ENDING COUNT(*) PERCENT
ABC_CUST_INFO_D   D       20      .00010
BBC_CUST_ENTRY_F  F       100     .030
FHS_PRDCT_STST_T  T       194     .031
GHS_INVTR_CD_DRY  DRY     493     .051

so after revisiting both the queries i am able to compare the counts and noticed there are count differences ( 1203 counts ), can some one please let me know why there are differences if i check the counts only by owner?因此,在重新访问这两个查询之后,我能够比较计数并注意到存在计数差异( 1203 计数),有人可以告诉我如果我仅按所有者检查计数为什么会有差异吗?

so can you please double check and let me know the query logic used for both Query1 and Query2 are correct?所以请您仔细检查并让我知道用于 Query1 和 Query2 的查询逻辑是否正确?

Your requirement is not that clear.你的要求不是明确。 If you want, for each owner, the count of objects according to the last portion of their name (the part that follows the last underscore, or the entire name if there is no underscore at all), you can do:如果您想要,对于每个所有者,根据其名称的最后部分(最后一个下划线之后的部分,或者如果根本没有下划线则为整个名称)计算对象的数量,您可以执行以下操作:

select owner, regexp_substr(object_name, '[^_]*$') ending, count(*) cnt,
    count(*) / sum(count(*)) over(partition by owner) ratio
from dba_objects
group by owner, regexp_substr(object_name, '[^_]*$')

You can easily adapt the query to match similar requirements: for example if you wanted the breakdown by object type, you would add object_type to the select and group by clauses, and to the partition by clause of the window count.您可以轻松调整查询以匹配类似的要求:例如,如果您想要按 object 类型进行细分,您可以将object_type添加到selectgroup by子句,以及partition by window 计数的子句。

Note that if you happen to have objects whose name ends with an underscore, ending will be computed as the empty string;请注意,如果您碰巧有名称以下划线结尾的对象,则ending将被计算为空字符串; this might, or might not be what you want.这可能是,也可能不是你想要的。

You also mentioned each object name in the expected result, you must use the analytical function as follows:您还在预期结果中提到了每个 object 名称,您必须使用解析 function,如下所示:

SELECT OBJECT_NAME,
       Sbstr as ending,
       Count(1) over (partition by sbstr) as "count(*)",
       Count(1) over (partition by sbstr) / Count(1) over () as percentage
From
(select
   object_name,
   REGEXP_SUBSTR(OBJECT_NAME, '_([A-Z]+)$',1,1,null,1) as SBSTR
from
   dba_objects 
Where 
   REGEXP_LIKE(OBJECT_NAME, '_[A-Z]$') 
)

Note: this will return every object names ending with upper case characters followed by _.注意:这将返回每 object 个以大写字符结尾且后跟 _ 的名称。

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

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