简体   繁体   中英

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

In order to keep simple my actual and clear question, please find below two queries

Query1 - Capture the counts of database objects BEGINNING with

owner counts - 70678

object_name counts - 121341

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

owner counts - 71881

object_name counts - 121341

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?

so can you please double check and let me know the query logic used for both Query1 and Query2 are correct?

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.

Note that if you happen to have objects whose name ends with an underscore, ending will be computed as the empty string; 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:

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 _.

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.

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