sql >> Databáze >  >> RDS >> Oracle

jak získat tento výsledek pomocí dotazu Oracle v hierarchické struktuře

Myslím, že se mi to podařilo pomocí analytických funkcí (ne elegantně, ale přesto) -

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))

Viz sqlfiddle na http://sqlfiddle.com/#!4/cfb1a7/40/ 0




  1. PHP a mySQL:Rok 2038 Chyba:Co to je? jak to vyřešit?

  2. Vytvořte databázi MySQL

  3. Ukládání vnořených dat JSON do databáze MySQL pomocí Hibernate

  4. nahradit přesnou shodu v php