2011-01-07 49 views
6

我有一個包含大量列和類型列的表。Oracle:查找只有空值的列

對於特定類型,某些列似乎總是空的。

我想爲每種類型創建一個視圖,並只顯示每種類型的相關列。假設一個列只有一個特定類型的空值,那麼這個列不應該成爲視圖的一部分,你怎麼能夠通過查詢發現這些?

是否有 SELECT [COLUMNNAME] FROM [表],其中[columnValues]都是[空]

我知道我完全由這一切之上......我只是想獲得的想法跨越。 在此先感謝!

+0

所以,你想要一個查詢只會返回實際上有數據的40列?大概如果其他10個獲得一個值,你的查詢將返回41列? – MartW 2011-01-07 16:58:57

+0

這聽起來像是你在同一個表中有不同的「種類」或「類型」的記錄,你希望每個記錄都有一個單獨的視圖。是否有列標識記錄的「類型」? – 2011-01-07 20:46:04

+0

是的@CodeByMoonlight是正確的。 – 2011-01-21 15:56:07

回答

0

是這樣的?

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

很顯然,如果你喜歡,也可以使用在CREATE VIEW...聲明。

0

看過@Gerrat和@ BQ的評論後,我可以通過以下方式獲得所需的詳細信息:我有一個具有N種不同類型的舊錶。所有類型共享列,並有專屬列。

我可以爲所有列創建每種類型的視圖,然後使用all_tab_columns獲取「num_nulls」小於該特定類型的總行數的所有列名稱。

從那裏應該很容易收集每個類型使用的列並創建視圖。

想法?

2
select 
    count(col_1), 
    count(col_2), 
    count(col_3) 
from 
    <table> 

返回每列多條記錄如何具有非空值(至少在Oracle中,這是。)

例如

drop table tq84_count_nulls; 

create table tq84_count_nulls (
    col_1 varchar(50), 
    col_2 number, 
    col_3 date 
); 

insert into tq84_count_nulls values (null, null, null); 
insert into tq84_count_nulls values ('xx', null, null); 
insert into tq84_count_nulls values (null, 42, null); 
insert into tq84_count_nulls values ('yy', 12, null); 

select 
    count(col_1), 
    count(col_2), 
    count(col_3) 
from 
    tq84_count_nulls; 

返回

COUNT(COL_1) COUNT(COL_2) COUNT(COL_3) 
------------ ------------ ------------ 
      2   2   0 

表明col_3只包含空值。

這個想法可以用來創建所需的視圖。

現在,表也需要* GROUP_ID *:

drop table tq84_count_nulls; 
create table tq84_count_nulls (
    col_1 varchar(50), 
    col_2 number, 
    col_3 date, 
    group_id varchar(2) 
); 

insert into tq84_count_nulls values (null, null, null, 'a'); 
insert into tq84_count_nulls values ('xx', null, null, 'a'); 
insert into tq84_count_nulls values (null, 42, null, 'a'); 
insert into tq84_count_nulls values ('yy', 12, null, 'a'); 

insert into tq84_count_nulls values (null, null, null, 'b'); 
insert into tq84_count_nulls values (null, null, null, 'b'); 
insert into tq84_count_nulls values (null, 42, null, 'b'); 
insert into tq84_count_nulls values (null, 12, null, 'b'); 




create or replace view nulls_per_type as 
with n as (
    select 
    count(col_1) col_1_count, 
    count(col_2) col_2_count, 
    count(col_3) col_3_count, 
    group_id 
    from 
    tq84_count_nulls 
    group by 
    group_id 
), 
o as (
select case col_1_count when 0 then 'COL_1 is always 0 for ' || group_id else null end u from n union all 
select case col_2_count when 0 then 'COL_2 is always 0 for ' || group_id else null end u from n union all 
select case col_3_count when 0 then 'COL_3 is always 0 for ' || group_id else null end u from n 
) 
select * from o where u is not null; 

,當選擇了回報:

select * from nulls_per_type; 

COL_1 is always 0 for b 
COL_3 is always 0 for a 
COL_3 is always 0 for b 
0

我想你可以解決這個使用元編程。使用遊標遍歷每個類型和列,並使用'not exists'來檢查列是否爲空。例如:

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 

道歉,如果有一個在語法錯誤的地方,我沒有檢查這個上。

9
SELECT t.column_name 
FROM user_tab_columns t 
WHERE t.nullable = 'Y' 
     AND t.table_name = 'YOUR_TABLE_NAME' 
     AND t.num_distinct = 0 
0

SELECT TableColumn的,tablecolumn2,... FROM TABLENAME 如果你有50列,其中10只包含空值WHERE列IS NOT NULL