Вы можете сделать это с помощью union all
(наряду с другими методами):
select rec_id, 'col1' as type, col1 as value from t where col1 is not null union all
select rec_id, 'col2' as type, col2 as value from t where col2 is not null union all
select rec_id, 'col3' as type, col3 as value from t where col3 is not null;
Вы кажется, хотят, чтобы устранить обе строки и столбцы, где любой NULL
появляется:
select rec_id, type, value
from (select t.*,
(case when count(*) over (partition by type) = count(value) over (partition by type)
then 1 else 0
end) as no_null_type,
(case when count(*) over (partition by recid) = count(value) over (partition by recid)
then 1 else 0
end) as no_null_recid
from (select rec_id, 'col1' as type, col1 as value from t union all
select rec_id, 'col2' as type, col2 as value from t union all
select rec_id, 'col3' as type, col3 as value from t
) t
) t
where no_null_type = 1 and no_null_recid = 1;
в Oracle 12c, вы можете использовать боковые присоединяется:
select t.rec_id, x.type, x.value
from t cross apply
(select 'col1' as type, col1 as value from dual union all
select 'col2' as type, col2 as value from dual union all
select 'col3' as type, col3 as value from dual
) x
where value is not null;
Это имеет то преимущество, что однократно сканирует таблицу. Вы можете применить аналогичную логику для удаления NULL
.
. , Я не уверен на 100%, я понимаю состояние «NULL». Является ли намерение просто отфильтровывать значения «NULL»? –
Да, если есть какое-либо значение NULL для любого столбца таблицы A, то оно не должно отображаться в таблице B в столбцах типа и значения – CodingFreak
Что такое «это»? Ряд? Колонка? –