我有一个包含大量列和类型列的表。
对于特定类型,某些列似乎总是为空。
我想为每种类型创建一个视图,并仅显示每种类型的相关列。假设如果一列只有特定类型的空值,那么这些列不应该是视图的一部分,你怎么能用查询找到它?
有没有 SELECT [columnName] FROM [table] WHERE [columnValues] ARE ALL [null]
我知道我完全已经完成了这一切......我只是试图让这个想法得以实现。 提前谢谢!
答案 0 :(得分:10)
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
答案 1 :(得分:3)
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
答案 2 :(得分:0)
在查看@Gerrat和@BQ的评论之后,我可以通过以下方式获得我需要的详细信息:我有一个具有N种不同类型的遗留表。所有类型共享列,并具有独占列。
我可以为所有列创建每个类型的视图,然后使用all_tab_columns获取“num_nulls”小于该特定类型的总行数的所有列名。
从那里收集用于每种类型的列并创建视图应该很容易。
思想?
答案 3 :(得分: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
道歉,如果某处语法出错,我无需查看。
答案 4 :(得分:0)
您可以使用以下查询进行标识:
从*中选择*(从test_null_col t中选择ascii(t.col2)+ ascii(t.col4)+ ascii(t.col1)+ ascii(t.col3)col) 其中col为null;
您想在此处删除查询的空列行
删除自 (从test_null_col t中选择ascii(t.col2)+ ascii(t.col4)+ ascii(t.col1)+ ascii(t.col3)col) 其中col为null;
答案 5 :(得分:0)
要查找具有空值的行,请使用“ 为空”条件。
select * from table_A where table_col1 is null;
要执行相反的操作,并找到所有具有非null值的行,请使用“ 不为空”条件:
select * from table_A where table_col1 is not null;
范围比较为空:
select * from table_A where table_col1 < 15 or table_col1 is null;
答案 6 :(得分:-1)
这样的东西?
SELECT column1, column2, column3 -- and so on
FROM tableA
WHERE columnX IS NULL
AND columnY IS NULL
AND columnZ IS NULL;
显然,如果您愿意,也可以在CREATE VIEW...
语句中使用它。
答案 7 :(得分:-1)
SELECT tablecolumn,tablecolumn2,... FROM TABLENAME 列IS IS NOT NULL