Oracle: Finding Columns with only null values

前端 未结 8 2075
温柔的废话
温柔的废话 2021-01-17 21:11

I have a table with a lot of columns and a type column.

Some columns seem to be always empty for a specific type.

I want to create a view for each type and o

相关标签:
8条回答
  • 2021-01-17 21:27

    Something like this?

    SELECT column1, column2, column3 -- and so on
    FROM tableA
    WHERE columnX IS NULL
    AND columnY IS NULL
    AND columnZ IS NULL;
    

    Obviously, you can use that in a CREATE VIEW... statement if you like as well.

    0 讨论(0)
  • 2021-01-17 21:30

    After looking at @Gerrat and @BQ's comments, I thouht I could get the details I need in the following way: I have a legacy table that has N different types. All types share columns, and have exclusive columns.

    I can create a view for each type with all columns, then use all_tab_columns to get all column names where the "num_nulls" is less than the total number of rows for that specific type.

    From there it should be easy to gather columns that are used for each type and create the views.

    Thoughts?

    0 讨论(0)
  • 2021-01-17 21:33

    You can identify by using the below query :

    select * from (select ascii(t.col2)+ascii(t.col4)+ascii(t.col1)+ascii(t.col3) col from test_null_col t)
    where col is null;
    

    And you want to delete null column rows here is query :

    delete from
    (select ascii(t.col2)+ascii(t.col4)+ascii(t.col1)+ascii(t.col3) col from test_null_col t)
    where col is null;
    
    0 讨论(0)
  • 2021-01-17 21:46
    SELECT tablecolumn, tablecolumn2, ... FROM TABLENAME
    WHERE column IS NOT NULL
    
    0 讨论(0)
  • 2021-01-17 21:51

    I think you can solve this using metaprogramming. Use a cursor to loop through each type and column, and use 'not exists' to check if the column is empty. For example:

    CREATE TABLE result_table (type VARCHAR(50), column VARCHAR(50))
    
    CURSOR c IS
        SELECT COLUMN_NAME FROM ALL_TAB_COLS WHERE TABLE_NAME = &table_name;
    
    CURSOR ct IS
        SELECT DISTINCT type_name FROM &table_name;
    
    BEGIN
    
    FOR t in ct
    LOOP
        FOR r in c
        LOOP
            --If you're confused about how this works, replace 'EXECUTE IMMEDIATE'
            --with print or something and look at the output
            EXECUTE IMMEDIATE
                'INSERT INTO result_table SELECT ''' ||
                    t.type_name || ''', ''' || r.COLUMN_NAME ||
                    ''' FROM DUAL WHERE NOT EXISTS (SELECT 1 FROM ' || 
                    &table_name || ' WHERE t.type_name = ''' || t.type_name ||
                    ''' AND ' || r.COLUMN_NAME || ' IS NOT NULL);';
        END LOOP
    END LOOP
    
    SELECT * FROM result_table
    

    Apologies if there's a mistake in the syntax somewhere, I have nothing to check this on.

    0 讨论(0)
  • 2021-01-17 21:51

    To find rows that have a null-value, use the "is null" condition.

    select * from table_A where table_col1 is null;
    

    To do the reverse, and find all the rows with a non-null value, use the "is not null" condition:

    select * from table_A where table_col1 is not null;
    

    Nulls in range comparisons:

    select * from table_A where table_col1 < 15 or table_col1 is null;
    
    0 讨论(0)
提交回复
热议问题