Oracle
 sql >> डेटाबेस >  >> RDS >> Oracle

Oracle SQL विश्लेषणात्मक क्वेरी - पुनरावर्ती स्प्रेडशीट की तरह कुल चल रहा है

तो चलिए MODEL को खोलते हैं इस समस्या पर खंड (एक उपकरण जिसका रहस्य केवल उसकी शक्ति से अधिक है):

with data as (
  select date'2012-01-01' as mm,    800 as a from dual union all
  select date'2012-02-01' as mm,   1900 as a from dual union all
  select date'2012-03-01' as mm,   1750 as a from dual union all
  select date'2012-04-01' as mm, -20000 as a from dual union all
  select date'2012-05-01' as mm,    900 as a from dual union all
  select date'2012-06-01' as mm,   3900 as a from dual union all
  select date'2012-07-01' as mm,  -2600 as a from dual union all
  select date'2012-08-01' as mm,  -2600 as a from dual union all
  select date'2012-09-01' as mm,   2100 as a from dual union all
  select date'2012-10-01' as mm,  -2400 as a from dual union all
  select date'2012-11-01' as mm,   1100 as a from dual union all
  select date'2012-12-01' as mm,   1300 as a from dual
)
select mm, a, b
from (
  -- Add a dummy value for b, making it available to the MODEL clause
  select mm, a, 0 b
  from data
)
      -- Generate a ROW_NUMBER() dimension, in order to access rows by RN
model dimension by (row_number() over (order by mm) rn)
      -- Spreadsheet values / measures involved in calculations are mm, a, b
      measures (mm, a, b)
      -- A single rule will do. Any value of B should be calculated according to
      -- GREATEST([previous value of B] + [current value of A], 0)
      rules (
        b[any] = greatest(nvl(b[cv(rn) - 1], 0) + a[cv(rn)], 0)
      )

उपरोक्त पैदावार:

MM              A     B
01.01.2012    800   800
01.02.2012   1900  2700
01.03.2012   1750  4450
01.04.2012 -20000     0
01.05.2012    900   900
01.06.2012   3900  4800
01.07.2012  -2600  2200
01.08.2012  -2600     0
01.09.2012   2100  2100
01.10.2012  -2400     0
01.11.2012   1100  1100
01.12.2012   1300  2400


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. SQLcl (Oracle) का उपयोग करते समय किसी क्वेरी से INSERT कथन कैसे उत्पन्न करें

  2. विवरण ऑपरेशन के साथ कॉलम की टिप्पणियां कैसे दिखाएं

  3. ओरेकल जॉइन (एसक्यूएल जॉइन) क्या हैं?

  4. DBA_TABLES से गणना (*) बनाम NUM_ROWS द्वारा तालिका की Oracle पंक्ति गणना

  5. शून्य मानों वाले कॉलम अपडेट करें