sql >> Database teknologi >  >> RDS >> Oracle

hvordan man får dette resultat ved hjælp af oracle-forespørgsel i hierarkisk struktur

Jeg tror, ​​jeg har formået at gøre dette med analytiske funktioner (ikke elegant, men ikke desto mindre) -

with
grps as(
    select marker
    from(
        select collateral_num,
               loan_num,
               rownum as marker,
               case when lead(loan_num,1) over (order by collateral_num) <> loan_num
                         and lead(collateral_num,1) over (order by collateral_num) <> collateral_num
                    then 1
                    when lead(loan_num,1) over (order by collateral_num) is null
                    then 1
                    else null end as grp
          from  loan
         order by collateral_num
        )
    where grp is not null
       )
, marked as(
    select loan.*, grps.*, row_number() over(partition by marker order by collateral_num) as splitter
    from loan, grps
           )
select  collateral_num,
        min(collateral_num) over (partition by marker) as min_collateral_num,
        loan_num
from    marked y
where   splitter <= marker
    and (splitter > (select max(x.marker) from marked x where x.marker < y.marker)
     or marker = (select min(marker) from marked))

Se sqlfiddle på http://sqlfiddle.com/#!4/cfb1a7/40/ 0




  1. Hvordan opdeles streng og indsætte den som ny linje i samme tabel?

  2. Hvordan kan jeg stoppe en kørende MySQL-forespørgsel?

  3. Hvordan angiver man, at en kolonne ikke er nullbar, når man opretter en materialiseret visning?

  4. MySQL batch stmt med Statement.RETURN_GENERATED_KEYS