select
count(col_1),
count(col_2),
count(col_3)
from
<table>
lợi nhuận bao nhiêu hồ sơ cho mỗi cột có một giá trị khác null (ít nhất là trong Oracle, đó là.)
Ví dụ
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;
lợi nhuận
COUNT(COL_1) COUNT(COL_2) COUNT(COL_3)
------------ ------------ ------------
2 2 0
chỉ ra rằng col_3 chỉ bao gồm các giá trị rỗng.
Ý tưởng này sau đó có thể được sử dụng để tạo chế độ xem mong muốn.
Bảng bây giờ cũng cần * 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;
nào, khi lợi nhuận được chọn:
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
Vì vậy, nếu bạn có 50 cột, và 10 trong số đó chứa các giá trị chỉ NULL, bạn muốn có một truy vấn mà sẽ chỉ trở lại 40 cột thực sự có dữ liệu? Và có lẽ nếu một trong 10 người khác mua lại một giá trị, truy vấn của bạn sau đó sẽ trả lại 41 cột? – MartW
Có vẻ như bạn có các loại "bản ghi" hoặc "loại" khác nhau trong cùng một bảng, nơi bạn muốn có một chế độ xem riêng biệt cho từng loại. Có cột nào xác định "loại" bản ghi không? –
Vâng, đó chính xác là @CodeByMoonlight. –