当前位置: 首页 > 面试题库 >

动态添加零值记录以供后续AP使用,以实现分析功能

孟英光
2023-03-14
问题内容

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.*,
         (sum(sales) over (partition by name, r_group
                           order by year, month
                           rows between unbounded preceding and current row
                          ) -sales ) as opening,
         sum(sales) over (partition by name, r_group
                          order by year, month
                          rows between unbounded preceding and current row
                         ) as closing
  from data t
 order by year , month

输出将是:

year   |   month   |  name   |  r_group   | sales   |  opening  |  closing |
2007   |     04    |   'A'   |   fruit   |  5       |    0      |    5     |
2007   |     04    |   'Z'   | fruit     |  99      |   0       |    99   |
2008   |     05    |   'A'   | fruit     |  10      |   5       |    15    |
2008   |     07    |   'B'   | vegetable |  20      |    0      |    20    |

如果我现在使用以下方法在此select语句之上进行聚合:

select year, month, r_group, sum(sales) sales, sum(opening) opening, sum(closing) closing from (
   select t.*, 
      (sum(sales) over........ 
) 
group by year, month, r_group
order by year, month

我得到以下结果:

year   |   month   |  r_group   | sales   |  opening   |  closing |
2007   |     04    |   fruit    |  104    |    0       |    104   |
2008   |     05    |   fruit    |  10     |    5       |    15    |
2008   |     07    | vegetable  |  20     |    0       |    20    |

这是 错误的 。请注意,2008年根本没有考虑过name =’Z’的值。由于累积函数是向后工作的,因此在2008年没有向后追溯的name
=’Z’记录。如果我在2008年输入零值记录,名称=’Z’,那么它将起作用。我想避免添加虚拟零值记录,并在查询中动态完成此操作。如果我在数据中添加零值记录,如下所示:

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, 'Z' name, 'fruit' r_group, '2008', year '05' month, 0 sales from dual union all
select 5 id, 'B' name, 'vegetable' r_group, '2008', year '07' month, 20 sales from dual ))

那么第一个查询将输出:

year   |   month   |  name   |  r_group   | sales   |  opening  |  closing |
2007   |     04    |   'A'   |   fruit   |  5       |    0      |    5     |
2007   |     04    |   'Z'   | fruit     |  99      |   0       |    99   |
2008   |     05    |   'A'   | fruit     |  10      |   5       |    15    |
2008   |     05    |   'Z'   | fruit     |  0       |   99      |    99    |
2008   |     07    |   'B'   | vegetable |  20      |    0      |    20    |

如果我再次使用第二个外部选择聚合,我将得到:

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    |

哪个是正确的。但是,正如我提到的,我不想添加零值记录。这里仅就此主题进行讨论:https
:
//asktom.oracle.com/pls/asktom/f?p=100
:11:0
:::::P11_QUESTION_ID
:
8912311513313但我无法使其工作。


问题答案:

一种相当简单的方法(类似于AskTom链接显示的方法)是提取所有年/月对,以及所有名称/ r_group对,然后将它们交叉连接:

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 a.year, a.month, b.name, b.r_group, nvl(d.sales, 0) as sales
from (select distinct year, month from data) a
cross join (select distinct name, r_group from data) b
left join data d on d.year = a.year and d.month = a.month and d.name = b.name and d.r_group = b.r_group
order by year, month, name, r_group;

YEAR MO N R_GROUP        SALES
---- -- - --------- ----------
2007 04 A fruit              5
2007 04 B vegetable          0
2007 04 Z fruit             99
2008 05 A fruit             10
2008 05 B vegetable          0
2008 05 Z fruit              0
2008 07 A fruit              0
2008 07 B vegetable         20
2008 07 Z fruit              0

但这会产生比您进行一级聚合所需的更多行:

YEAR MO N R_GROUP        SALES    OPENING    CLOSING
---- -- - --------- ---------- ---------- ----------
2007 04 A fruit              5          0          5
2007 04 B vegetable          0          0          0
2007 04 Z fruit             99          0         99
2008 05 A fruit             10          5         15
2008 05 B vegetable          0          0          0
2008 05 Z fruit              0         99         99
2008 07 A fruit              0         15         15
2008 07 B vegetable         20          0         20
2008 07 Z fruit              0         99         99

当与您的第二个级别(来自另一个查询)进行汇总时,将为例如2007/04 / vegetable生成额外的行:

YEAR MO R_GROUP        SALES    OPENING    CLOSING
---- -- --------- ---------- ---------- ----------
2007 04 fruit            104          0        104
2007 04 vegetable          0          0          0
2008 05 fruit             10        104        114
2008 05 vegetable          0          0          0
2008 07 fruit              0        114        114
2008 07 vegetable         20          0         20

您可以在汇总之前将其部分过滤掉,因为所有中间列都为零:

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 year,
       month,
       r_group,
       sum(sales) sales,
       sum(opening) opening,
       sum(closing) closing
from (
  select t.*,
         (sum(sales) over (partition by name, r_group
                           order by year, month
                           rows between unbounded preceding and current row
                          ) -sales ) as opening,
         sum(sales) over (partition by name, r_group
                          order by year, month
                          rows between unbounded preceding and current row
                         ) as closing
  from (
    select a.year, a.month, b.name, b.r_group, nvl(d.sales, 0) as sales
    from (select distinct year, month from data) a
    cross join (select distinct name, r_group from data) b
    left join data d
    on d.year = a.year and d.month = a.month and d.name = b.name and d.r_group = b.r_group
  ) t
)
where sales != 0 or opening != 0 or closing != 0
group by year, month, r_group
order by year, month;

要得到:

YEAR MO R_GROUP        SALES    OPENING    CLOSING
---- -- --------- ---------- ---------- ----------
2007 04 fruit            104          0        104
2008 05 fruit             10        104        114
2008 07 fruit              0        114        114
2008 07 vegetable         20          0         20

您可以进一步过滤结果,以删除汇总销售价值仍为零的行,尽管如果这样做的话,则不再需要 汇总 之前
进行过滤;但还是有点混乱。尚不清楚是否可以修改最外部的聚合来做到这一点。



 类似资料:
  • 本文向大家介绍Angularjs 实现动态添加控件功能,包括了Angularjs 实现动态添加控件功能的使用技巧和注意事项,需要的朋友参考一下 实现下面这样的需求: 点击增加一块数据盘,会出现数据盘选项。 (1)最开始,想到原生JavaScript,jQuery (appendChild()等方法结合AngularJS来添加新的元素。但是突然发现控件里面的数据绑定,原生javascript没法控制

  • 本文向大家介绍JSP实现添加功能和分页显示实例分析,包括了JSP实现添加功能和分页显示实例分析的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了JSP实现添加功能和分页显示的方法。分享给大家供大家参考。具体如下: 学习目标: ① 进一步掌握MVC设计模式; ② 掌握添加功能的实现; ③ 掌握分页显示功能的实现。 主要内容: ① 通过用户信息添加功能进一步介绍MVC模式; ② 通过用户信息的分

  • 问题内容: 我实质上是试图浏览html文件的文件夹。我想将它们嵌入到二进制文件中,并能够根据请求解析它们以用于模板执行目的。(如果我的措词不正确,请原谅)。 任何想法,技巧,窍门或实现此目的的更好方法将不胜感激。 问题答案: 我使用大多数Go Web应用程序执行此操作。我使用go-bindata从要嵌入的所有文件中自动生成Go源代码,然后将其编译为二进制文件。所有这些都是在构建过程中自动完成的。

  • 本文向大家介绍Android中实现记事本动态添加行效果,包括了Android中实现记事本动态添加行效果的使用技巧和注意事项,需要的朋友参考一下 本文主要给大家介绍了关于Android实现记事本动态添加行的相关内容,分享出来供大家参考学习,下面来一起看看详细的介绍: 先看效果图: 这是昨天在群里面有人在问这个问题,在这里顺便记录一下,这个效果我们可以自定义EditText,实现起来也不难 看详细步骤

  • 问题内容: 我想使用python下载和解析网页,但是要访问它,我需要设置一些cookie。因此,我需要先通过https登录到网页。登录时刻涉及将两个POST参数(用户名,密码)发送到/login.php。在登录请求期间,我想从响应头中检索cookie并将其存储,以便可以在请求中使用它们来下载/data.php网页。 我将如何在python(最好是2.6)中做到这一点?如果可能,我只想使用内置模块。