如何计算oracle sql中表的所有列中零的数量

发布于 2025-01-17 01:23:36 字数 1148 浏览 0 评论 0原文

假设您有一个名为 db_table1 的数据库表,其中包含数百列,其中大多数包含大量 0。

我试图计算每列的零数并将其除以列的长度,以便我可以看到每列中零的比率。

下面的代码给出了每列中空值的比率。但我无法将其修改为计数 0 而不是空值

任何帮助将不胜感激

SELECT column_name, (num_nulls/ (SELECT COUNT(*) FROM db_table)*100)
FROM all_tab_columns 
WHERE table_name='table’

示例数据:

col_1col_2col_3
0.0000.0000.000
0.0000.00014.857
3.5480.0000.000
0.0000.0000.000
0.0000.0000.000
0.00011.5860.000

预期输出:

All_ColumnsRatio_of_Zeros
col_165.5
col_273.5
col_348.6

简而言之,我需要每列中的零总数除以总行数,这给出了每列中零的百分比或比率,这样我就可以像这样对待零null 并消除该列(如果超过)百分之 60 的零可以说...

Suppose you have a database table named db_table1 with hundreds of columns and most of them contain lots of 0's.

I'm trying to count zeros for each column and divide it by the length of column so I can see the ratio of zeros in each column.

The below code gives me the ratio of nulls in each column. But I couldn't revise it to count 0's instead of nulls

Any help will be appreciated

SELECT column_name, (num_nulls/ (SELECT COUNT(*) FROM db_table)*100)
FROM all_tab_columns 
WHERE table_name='table’

Sample data:

col_1col_2col_3
0.0000.0000.000
0.0000.00014.857
3.5480.0000.000
0.0000.0000.000
0.0000.0000.000
0.00011.5860.000

Expected Output:

All_ColumnsRatio_of_Zeros
col_165.5
col_273.5
col_348.6

Briefly I need the total number of zeros in every column divided by total row number which gives me the percent or ratio of zeros in each column so I can treat zeros like null and eliminate the column if it has more than 60 percent zeros lets say...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗琴音 2025-01-24 01:23:36

您可以使用:


WITH AA AS (
select 
col_1,col_2,col_3
from table_name
)

SELECT column_name, COUNT(column_name) zeros FROM
(
SELECT * FROM (

SELECT * FROM AA
UNPIVOT(
    zeros  
    FOR column_name 
    IN ( 
col_1,col_2,col_3
    )
)
) WHERE zeros = 0
) GROUP BY column_name

You can use:


WITH AA AS (
select 
col_1,col_2,col_3
from table_name
)

SELECT column_name, COUNT(column_name) zeros FROM
(
SELECT * FROM (

SELECT * FROM AA
UNPIVOT(
    zeros  
    FOR column_name 
    IN ( 
col_1,col_2,col_3
    )
)
) WHERE zeros = 0
) GROUP BY column_name

守不住的情 2025-01-24 01:23:36

您可以使用:

SELECT 'SELECT ''' || table_name || ''' AS table_name, ' ||
               '''' || column_name || ''' AS column_name, ' ||
               '100 - SUM(LENGTH(REPLACE(TO_CHAR("'|| column_name || '", ''fm999999999999999990.000''), ''0''))-1)' ||
               ' / SUM(LENGTH(TO_CHAR("'|| column_name || '", ''fm999999999999999990.000''))-1) * 100 AS ratio_of_zeroes ' ||
       'FROM "' || table_name || '" UNION ALL'
       column_name
FROM   user_tab_columns
WHERE  data_type = 'NUMBER';

生成 SQL 语句并从最后一行中删除 UNION ALL 并执行它。

如果您有表:

CREATE TABLE table_name (col_1, col_2, col_3) AS
SELECT 0.000,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000,  0.000, 14.857 FROM DUAL UNION ALL
SELECT 3.548,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000, 11.586,  0.000 FROM DUAL;

那么生成的语句(删除最后的 UNION ALL)为:

SELECT 'TABLE_NAME' AS table_name, 'COL_1' AS column_name, 100 - SUM(LENGTH(REPLACE(TO_CHAR("COL_1", 'fm999999999999999990.000'), '0'))-1) / SUM(LENGTH(TO_CHAR("COL_1", 'fm999999999999999990.000'))-1) * 100 AS ratio_of_zeroes FROM "TABLE_NAME" UNION ALL
SELECT 'TABLE_NAME' AS table_name, 'COL_2' AS column_name, 100 - SUM(LENGTH(REPLACE(TO_CHAR("COL_2", 'fm999999999999999990.000'), '0'))-1) / SUM(LENGTH(TO_CHAR("COL_2", 'fm999999999999999990.000'))-1) * 100 AS ratio_of_zeroes FROM "TABLE_NAME" UNION ALL
SELECT 'TABLE_NAME' AS table_name, 'COL_3' AS column_name, 100 - SUM(LENGTH(REPLACE(TO_CHAR("COL_3", 'fm999999999999999990.000'), '0'))-1) / SUM(LENGTH(TO_CHAR("COL_3", 'fm999999999999999990.000'))-1) * 100 AS ratio_of_zeroes FROM "TABLE_NAME";

执行的输出为:

<表类=“s-表”>
<标题>

TABLE_NAME
COLUMN_NAME
RATIO_OF_ZEROES


<正文>

TABLE_NAME
COL_1
83.33333333333333333333333333333333333333

TABLE_NAME
COL_2
80

TABLE_NAME
COL_3
80

db<>fiddle 此处

You can use:

SELECT 'SELECT ''' || table_name || ''' AS table_name, ' ||
               '''' || column_name || ''' AS column_name, ' ||
               '100 - SUM(LENGTH(REPLACE(TO_CHAR("'|| column_name || '", ''fm999999999999999990.000''), ''0''))-1)' ||
               ' / SUM(LENGTH(TO_CHAR("'|| column_name || '", ''fm999999999999999990.000''))-1) * 100 AS ratio_of_zeroes ' ||
       'FROM "' || table_name || '" UNION ALL'
       column_name
FROM   user_tab_columns
WHERE  data_type = 'NUMBER';

To generate an SQL statement and remove the UNION ALL from the last line and execute it.

If you have the table:

CREATE TABLE table_name (col_1, col_2, col_3) AS
SELECT 0.000,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000,  0.000, 14.857 FROM DUAL UNION ALL
SELECT 3.548,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000,  0.000,  0.000 FROM DUAL UNION ALL
SELECT 0.000, 11.586,  0.000 FROM DUAL;

Then the statement generated (with the final UNION ALL removed) is:

SELECT 'TABLE_NAME' AS table_name, 'COL_1' AS column_name, 100 - SUM(LENGTH(REPLACE(TO_CHAR("COL_1", 'fm999999999999999990.000'), '0'))-1) / SUM(LENGTH(TO_CHAR("COL_1", 'fm999999999999999990.000'))-1) * 100 AS ratio_of_zeroes FROM "TABLE_NAME" UNION ALL
SELECT 'TABLE_NAME' AS table_name, 'COL_2' AS column_name, 100 - SUM(LENGTH(REPLACE(TO_CHAR("COL_2", 'fm999999999999999990.000'), '0'))-1) / SUM(LENGTH(TO_CHAR("COL_2", 'fm999999999999999990.000'))-1) * 100 AS ratio_of_zeroes FROM "TABLE_NAME" UNION ALL
SELECT 'TABLE_NAME' AS table_name, 'COL_3' AS column_name, 100 - SUM(LENGTH(REPLACE(TO_CHAR("COL_3", 'fm999999999999999990.000'), '0'))-1) / SUM(LENGTH(TO_CHAR("COL_3", 'fm999999999999999990.000'))-1) * 100 AS ratio_of_zeroes FROM "TABLE_NAME";

And the output of executing that is:

TABLE_NAMECOLUMN_NAMERATIO_OF_ZEROES
TABLE_NAMECOL_183.33333333333333333333333333333333333333
TABLE_NAMECOL_280
TABLE_NAMECOL_380

db<>fiddle here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文