You appear to want, for each combination of other column values, to have the text associated with a null bltugp
to be prepended to the text associated with all non-null values.
One way to do that is with concatenation and an analytic function to find the null-based text to prepend:
max(case when bltugp is null then bltxt end)
over (partition by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz)
|| bltxt
which would give you three rows:
BLTGS1 BLTGS2 BLTGS3 BL BLT BLTGS6 BL TEXT
---------- ---------- ---------- -- --- ------ -- --------------------------------------------------------------------------------------------------------
2 1 12 01 007 01 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von bis zu einem Jahr
2 1 12 01 007 02 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von mehr als einem Jahr
2 1 12 01 007 erhaltene Anzahlungen auf Bestellungerhaltene Anzahlungen auf Bestellung
... and then discard the one you don't want. As an example with those and a couple of other maybe-interesting rows as sample data:
-- CTE for dummy data
with bip105 (bltspriso, blttkz, bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, bltugp, bltxt) as (
select 'DEAT', 42, 2, 1, 12, '01', '006', null, '02', 'davon mit einer Restlaufzeit von mehr als einem Jahr' from dual
union all
select 'DEAT', 42, 2, 1, 12, '01', '007', null, null, 'erhaltene Anzahlungen auf Bestellung' from dual
union all
select 'DEAT', 42, 2, 1, 12, '01', '007', null, '01', 'davon mit einer Restlaufzeit von bis zu einem Jahr' from dual
union all
select 'DEAT', 42, 2, 1, 12, '01', '007', null, '02', 'davon mit einer Restlaufzeit von mehr als einem Jahr' from dual
union all
select 'DEAT', 42, 2, 1, 12, '01', '021', null, null, 'sonstige Verbindlichkeiten' from dual
)
-- actual query
select bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, bltugp, text
from (
select bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz, bltugp
, max(case when bltugp is null then bltxt end)
over (partition by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz)
|| bltxt as text
from bip105
where bltspriso = 'DEAT'
)
where bltugp is not null
order by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz, bltugp;
BLTGS1 BLTGS2 BLTGS3 BL BLT BLTGS6 BL TEXT
---------- ---------- ---------- -- --- ------ -- --------------------------------------------------------------------------------------------------------
2 1 12 01 006 02 davon mit einer Restlaufzeit von mehr als einem Jahr
2 1 12 01 007 01 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von bis zu einem Jahr
2 1 12 01 007 02 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von mehr als einem Jahr
If you want to show all of the null-based rows as well you can remove the filter, but also need to stop that value being prepended to itself; probably simpler ways but this is one using another case expression:
select bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, bltugp, text
from (
select bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz, bltugp
, max(case when bltugp is null then bltxt end)
over (partition by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz)
|| case when bltugp is not null then bltxt end as text
from bip105
where bltspriso = 'DEAT'
)
order by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz, bltugp
BLTGS1 BLTGS2 BLTGS3 BL BLT BLTGS6 BL TEXT
---------- ---------- ---------- -- --- ------ -- --------------------------------------------------------------------------------------------------------
2 1 12 01 006 02 davon mit einer Restlaufzeit von mehr als einem Jahr
2 1 12 01 007 01 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von bis zu einem Jahr
2 1 12 01 007 02 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von mehr als einem Jahr
2 1 12 01 007 erhaltene Anzahlungen auf Bestellung
2 1 12 01 021 sonstige Verbindlichkeiten
Or if you only want to see the null-based rows when there are no others for that combination, you could count the non-null entries for each combination, and only exclude the nulls if any non-null exist (implying the null one has been prepended and isn't needed on its own - if that is indeed the rule you need):
select bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, bltugp, text
from (
select bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz, bltugp
, max(case when bltugp is null then bltxt end)
over (partition by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz)
|| case when bltugp is not null then bltxt end as text
, count(bltugp) over (partition by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz)
as non_null_count
from bip105
where bltspriso = 'DEAT'
)
where non_null_count = 0 or bltugp is not null
order by bltgs1, bltgs2, bltgs3, bltgs4, bltgs5, bltgs6, blttkz, bltugp;
BLTGS1 BLTGS2 BLTGS3 BL BLT BLTGS6 BL TEXT
---------- ---------- ---------- -- --- ------ -- --------------------------------------------------------------------------------------------------------
2 1 12 01 006 02 davon mit einer Restlaufzeit von mehr als einem Jahr
2 1 12 01 007 01 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von bis zu einem Jahr
2 1 12 01 007 02 erhaltene Anzahlungen auf Bestellungdavon mit einer Restlaufzeit von mehr als einem Jahr
2 1 12 01 021 sonstige Verbindlichkeiten
It still isn't really clear what you want to see...