gpt4 book ai didi

sql - Oracle SQL 分析查询 - 递归电子表格式运行总计

转载 作者:行者123 更新时间:2023-12-03 22:55:37 24 4
gpt4 key购买 nike

我有以下数据,由 A 组成值,按 MM 排序(月)。
B列计算为 GREATEST(current value of A + previous value of B, 0)以类似电子表格的方式。

我如何计算 B使用 SQL 查询?

  • 我尝试使用分析函数,但我无法成功。
  • 我知道有 Model Clause ;我找到了 a similar example ,但我不知道从哪里开始。

  • 我使用的是 Oracle 10g,因此我不能使用递归查询。

    这是我的测试数据:
    MM         | A      | B
    -----------+--------+------
    2012-01-01 | 800 | 800
    2012-02-01 | 1900 | 2700
    2012-03-01 | 1750 | 4450
    2012-04-01 | -20000 | 0
    2012-05-01 | 900 | 900
    2012-06-01 | 3900 | 4800
    2012-07-01 | -2600 | 2200
    2012-08-01 | -2600 | 0
    2012-09-01 | 2100 | 2100
    2012-10-01 | -2400 | 0
    2012-11-01 | 1100 | 1100
    2012-12-01 | 1300 | 2400

    这是“表定义”:
    select t.* from (
    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
    ) t;

    最佳答案

    所以让我们释放 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

    关于sql - Oracle SQL 分析查询 - 递归电子表格式运行总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12243488/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com