0

I have an Oracle table which contains columns labelled attribute1 to attribute15. I need to list any of those columns that contain any values.

I need to do something like;

select ... 
from table 
where column_name like 'attribute%' 
  and column is not null

Can't find anything resembling this

4
  • 1
    That sounds like the wrong database design. Commented Oct 7, 2019 at 14:18
  • Please show the exact definition of the table. Commented Oct 7, 2019 at 14:20
  • @a_horse_with_no_name He is probably using the Oracle e-Business Suite. Almost every table in that database has at least 15 ATTRIBUTE* columns. They're called "descriptive flexfields" and are actually very sensible and useful, especially for a database design that is 25+ years old. Commented Oct 7, 2019 at 14:22
  • Exactly right @MatthewMcPeak it's EBS. Commented Oct 7, 2019 at 15:04

3 Answers 3

1

Why get fancy?

Describe your table to find out how many ATTRIBUTE* columns there are and then write this:

SELECT *
FROM   table
WHERE  ATTRIBUTE1 IS NOT NULL
OR     ATTRIBUTE2 IS NOT NULL
OR     ATTRIBUTE3 IS NOT NULL
...

Or, if you need to know which columns in particular have values,

SELECT DECODE(COUNT(ATTRIBUTE1),0,'N','Y') ATTRIBUTE1_HAS_VALUES,
       DECODE(COUNT(ATTRIBUTE2),0,'N','Y') ATTRIBUTE2_HAS_VALUES,
       ...
FROM   TABLE;
Sign up to request clarification or add additional context in comments.

2 Comments

I know there are 15 attribute columns, I just wondered if there was a way to wildcard the attribute column rather than write it out 15 times. It's lazy I know, but if I have to do this discovery on multiple tables I'd like to make it as simple as possible.
There is no simple syntax for that. You can use dynamic SQL to write the queries and execute them. Depending on how well you know how to use dynamic SQL and how many tables you are analyzing, that may or may not be less work.
0

I think you have null values in most of the columns and need any non null values.

If aforementioned is correct understanding then you can use coalesce function as following.

Select coalesce(attribute1, attribute2, .... , attribute15) as val 
-- or use select * , if you want all columns value
From your_table
Where coalesce(attribute1, attribute2, .... , attribute15) is not null;

Cheers!!

1 Comment

It's more than likely that all the columns will be null, so yes it would be great to list any of them that have a value. To avoid listing all 15 columns is my main goal here
0

You may look at the table statistics to get this information.

  1. analyze the table without sampling (= full)

    exec dbms_stats.gather_table_stats(ownname=>user, tabname=>'TAB',estimate_percent=>100);
    
  2. query the NUM_DISTINCT column in user_tab_columns

    select COLUMN_NAME, NUM_DISTINCT, NUM_NULLS
    from user_tab_columns 
    where table_name = 'TAB';
    
    COLUMN_NAME                    NUM_DISTINCT  NUM_NULLS
    ------------------------------ ------------ ----------
    ATT1                                      0          9
    ATT2                                      3          3
    ATT3                                      9          0
    

Note that NUM_DISTINCT = 0 means that all values are NULL.

On the contrary NUM_NULLS = 0 identifies not null columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.