Один из вариантов - выбрать строку-заглушку заголовка с помощью NOT EXISTS
, чтобы решить, следует ли ее возвращать (да, если фактическая строка в таблице не существует; нет, если строка существует).
Посмотрите на пример: посмотрите строку # 4, которая в настоящий момент закомментирована, что означает, что ваша таблица не содержит строки, удовлетворяющей этому условию, поэтому фиктивная строка header будетбудет отображаться:
SQL> with test (type, some_id) as
2 (select 'Static 99', 44 from dual union all
3 select 'Static 2' , 57 from dual
4 --union all select 'Static 1', 66 from dual -- toggle to see the difference
5 )
6 -- This is a header which won't be displayed if table contains
7 -- type = 'Static 1' and some_id = 66
8 select 'Static 1' type, 1 some_id
9 from dual
10 where not exists (select null
11 from test
12 where type = 'Static 1'
13 and some_id = 66
14 )
15 union all
16 -- This returns actual rows (if they exist)
17 select 'Static 1' type, some_id
18 from (select some_id
19 from test
20 where type = 'Static 1'
21 and some_id = 66
22 );
TYPE SOME_ID
-------- ----------
Static 1 1
SQL>
Однако, если он существует (строка # 4 без комментариев), то отображаются фактические данные без этой строки заголовка-пустышки:
SQL> with test (type, some_id) as
2 (select 'Static 99', 44 from dual union all
3 select 'Static 2' , 57 from dual
4 union all select 'Static 1', 66 from dual -- toggle to see the difference
5 )
6 -- This is a header which won't be displayed if table contains
7 -- type = 'Static 1' and some_id = 66
8 select 'Static 1' type, 1 some_id
9 from dual
10 where not exists (select null
11 from test
12 where type = 'Static 1'
13 and some_id = 66
14 )
15 union all
16 -- This returns actual rows (if they exist)
17 select 'Static 1' type, some_id
18 from (select some_id
19 from test
20 where type = 'Static 1'
21 and some_id = 66
22 );
TYPE SOME_ID
-------- ----------
Static 1 66
SQL>
Если это делает то, что выхотите, примените тот же принцип ко второму select
, который вы используете (к Static 2
).