3

たとえば、クエリからのこの結果を単純化しました(select ... from ... where ...):

id months amount
1  2     120
1  6      180
2  2     120
2  6      180

ここで、上記の最初のテーブルから別のテーブルを取得する必要があり、次のようになります。

id months month_name amount
   2                 120
1  2      2013-04    60
1  2      2013-05    60
   6      180
1  6      2013-04     30
1  6      2013-05     30
1  6      2013-06     30
1  6      2013-07     30
1  6      2013-08     30
1  6      2013-09     30
   2      120
2  2      2013-04     60
2  2      2013-05     60
   6      180
2  6      2013-04     30
2  6      2013-05     30
2  6      2013-06     30
2  6      2013-07     30
2  6      2013-08     30
2  6      2013-09     30

この結果をクエリで取得する方法がわかりませんか? プロシージャを使用する必要がありますか? もしそうなら - 例はありますか?

編集: 今必要なようです: 指定された日付が sysdate の 1 か月以上前 (たとえば 2 か月) の場合、最初の金額を 2 倍にし、すべての分割を 1 か月少なくする必要があります。説明するのは難しい。たとえば ....add_months(to_date('2013-03','yyyy-mm'), Nl-1) as month_name... sysdate を 2013-04 とすると、結果は次のようになります。

id months month_name amount
   2                 120
1  2      2013-03    120
   6      180
1  6      2013-03     60
1  6      2013-04     30
1  6      2013-05     30
1  6      2013-06     30
1  6      2013-07     30
   2      120
2  2      2013-03     60
   6      180
2  6      2013-03     60
2  6      2013-04     30
2  6      2013-05     30
2  6      2013-06     30
2  6      2013-07     30
4

3 に答える 3

3

Oracle 11 では、再帰クエリを実行して取得できます。

with base_Query as (select * from test),
data (id, months, amount, iter, month_amount) 
  as (select id, months, amount, 0, amount/months
        from base_Query
      union all
      select id, months, amount, iter+1, month_amount
        from data
       where iter+1 <= months)
select case when iter = 0 then to_number(null) else id end id, 
       months, 
       case when iter = 0 then amount else month_amount end amount
  from data d
 order by d.id, months;

http://sqlfiddle.com/#!4/27edc/1

10g モデリング バリアントも:

with base_query as (select rownum r, t.*, amount/months monthly_amount from test t)
select case i when 0 then to_number(null) else id end id,
       months, amount
  from base_query
model
partition by (r)
dimension by (0 as i)
measures (months, amount, monthly_amount, id)
rules (
  id[for i from 0 to months[0] increment 1] = id[0],
  amount[any] = case cv(i) when 0 then amount[0] else monthly_amount[0] end,
  months[any] = months[0]
);

http://sqlfiddle.com/#!4/27edc/4

データに一意でない ID があったため、一意のアドレス指定システムを取得するために rownum を追加しました。

于 2013-04-08T08:26:11.500 に答える
3

9i 以降のバージョンでは、次のことができます。

select decode(flag, 'original', null, id), months, amount
from(
    select id, 
           months, 
           add_months(to_date('2013-04','yyyy-mm'), N.l-1) as month_name,
           amount/months as amount, 
           'splitted' as flag
    from your_table t
    join (select level l from dual connect by level < 1000) N
    on (t.months >= N.l)

    union all

    select id, months, amount, null, 'original'
    from your_table
)
order by id, months;
于 2013-04-08T08:28:22.380 に答える
0

あなたの質問は不完全なようです。Where 条件を削除して、テーブルからデータを取得したかっただけだと思います。「SELECT * FROM YOUR_TABLE_NAME」で試してみてください。

あなたの不完全な質問に私が正しければ、これはあなたが探していた結果を与えるはずです

于 2013-04-08T08:16:01.020 に答える