Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

cara mendapatkan hasil ini menggunakan kueri Oracle dalam struktur hierarki

Saya pikir saya telah berhasil melakukan ini dengan fungsi analitik (tidak elegan, tapi tetap saja) -

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

Lihat sqlfiddle di http://sqlfiddle.com/#!4/cfb1a7/40/ 0




  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. REGEXP_REPLACE - hapus baris baru \n dari string HANYA jika diapit dalam ()'s

  2. Cara bekerja dengan Fungsi tanggal di Oracle sql

  3. Buat indeks pada tabel yang ada Oracle

  4. menghubungkan oracle dari r

  5. Oracle:DDL dan pengembalian transaksi