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

Menambahkan catatan bernilai nol dalam kueri menggunakan fungsi analitik kumulatif

Alih-alih CURRENT ROW, Anda dapat menggunakan kata kunci PRECEDING untuk menjumlahkan hingga baris sebelumnya.

with data as (
  select 1 id, 'A' name, 'fruit' r_group, '2007' year, '04' month, 5 sales from dual union all
  select 2 id, 'Z' name, 'fruit' r_group, '2007' year, '04' month, 99 sales from dual union all
  select 3 id, 'A' name, 'fruit' r_group, '2008' year, '05' month, 10 sales from dual union all
  select 4 id, 'B' name, 'vegetable' r_group, '2008' year, '07' month, 20 sales from dual )
select t.*, 
  coalesce(sum(sales) over (partition by  r_group order by year, month rows between unbounded preceding and 1 preceding),0) opening,
  sum(sales) over (partition by  r_group order by year, month rows between unbounded preceding and current row) closing
from (
  select year, month, r_group, sum(sales) sales
  from data
  group by year, month, r_group
  ) t
order by 3,1,2;

year    month   r_group     sales   opening closing
---------------------------------------------------
2007    04      fruit       104     0       104
2008    05      fruit       10      104     114
2008    07      vegetable   20      0       20


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Konfigurasi JMeter JDBC Pool

  2. Bagaimana cara memanggil prosedur tersimpan Oracle yang menyertakan tipe yang ditentukan pengguna di java?

  3. Aktifkan fungsi dari DLL (c#) di Oracle Forms

  4. Oracle SQL- Tandai catatan berdasarkan tanggal catatan vs riwayat

  5. ATAU Database/Ketik di Oracle Express/SQL Developer