1

たとえば、次のデータがあります。

  select 1 id, date '2007-01-16' date_created, 5 sales, 'Bob' name from dual union all
  select 2 id, date '2007-04-16' date_created, 2 sales, 'Bob' name from dual union all
  select 3 id, date '2007-05-16' date_created, 6 sales, 'Bob' name from dual union all
  select 4 id, date '2007-05-21' date_created, 4 sales, 'Bob' name from dual union all
  select 5 id, date '2013-07-16' date_created, 24 sales, 'Bob' name from dual union all
  select 6 id, date '2007-01-17' date_created, 15 sales, 'Ann' name from dual union all
  select 7 id, date '2007-04-17' date_created, 12 sales, 'Ann' name from dual union all
  select 8 id, date '2007-05-17' date_created, 16 sales, 'Ann' name from dual union all
  select 9 id, date '2007-05-22' date_created, 14 sales, 'Ann' name from dual union all
  select 10 id, date '2013-07-17' date_created, 34 sales, 'Ann' name from dual

次のような結果を得たいです。

Name        Total_cumulative_sales      Total_sales_current_month
Bob         41                          24
Ann         91                          34

このテーブルでは、Bob の総売上は最初から 41 です。そして今月は 7 月で、彼のこの月全体の売り上げは 24 です。アンについても同じことが言えます。

この結果を取得するための SQL をどのように記述すればよいでしょうか?

4

3 に答える 3

2

これは、何年にもわたって販売に役立つはずです。任意の年数にわたる累積売上を取得します。直近の月に売上がない場合、レコードは生成されません。

    WITH sales AS 
(select 1 id, date '2007-01-16' date_created, 5 sales, 'Bob' sales_name from dual union all
  select 2 id, date '2007-04-16' date_created, 2 sales, 'Bob' sales_name from dual union all
  select 3 id, date '2007-05-16' date_created, 6 sales, 'Bob' sales_name from dual union all
  select 4 id, date '2007-05-21' date_created, 4 sales, 'Bob' sales_name from dual union all
  select 5 id, date '2013-07-16' date_created, 24 sales, 'Bob' sales_name from dual union all
  select 6 id, date '2007-01-17' date_created, 15 sales, 'Ann' sales_name from dual union all
  select 7 id, date '2007-04-17' date_created, 12 sales, 'Ann' sales_name from dual union all
  select 8 id, date '2007-05-17' date_created, 16 sales, 'Ann' sales_name from dual union all
  select 9 id, date '2007-05-22' date_created, 14 sales, 'Ann' sales_name from dual union all
  select 10 id, date '2013-07-17' date_created, 34 sales, 'Ann' sales_name from dual)
  SELECT  sales_name
         ,total_sales
         ,monthly_sales
         ,mon 
  FROM (SELECT sales_name
              ,SUM(sales) OVER (PARTITION BY sales_name ORDER BY mon) total_sales
              ,SUM(sales) OVER (PARTITION BY sales_name,mon ORDER BY mon) monthly_sales
              ,mon 
              ,max_mon
        FROM (  SELECT sales_name
                      ,sum(sales) sales
                      ,mon
                      ,max_mon
                FROM   (SELECT sales_name
                              ,to_number(to_char(date_created,'YYYYMM')) mon
                              ,sales
                              ,MAX(to_number(to_char(date_created,'YYYYMM'))) OVER (PARTITION BY sales_name) max_mon
                        FROM sales
                        ORDER BY 2)
                GROUP BY sales_name
                        ,max_mon
                        ,mon
             )
        )
WHERE max_mon = mon

;

于 2013-07-16T10:21:18.923 に答える