从动态输入数据中捕获未发生的数据

时间:2017-03-26 16:51:45

标签: sql oracle oracle11g

我有如下所示的数据规则

1|Group1|Mandatory|1st occurrence
2|Group1|Optional|1st occurrence
3|Group1|Mandatory|1st occurrence
1|Group1|Mandatory|2nd occurrence
2|Group1|Optional|2nd occurrence
3|Group1|Mandatory|2nd occurrence
4|Group2|Mandatory|1st occurrence
5|Group2|Mandatory|1st occurrence
6|Group2|Optional|1st occurrence

在这里,您可以看到组1对于数据记录1,2和3存在两次。这意味着组1可以出现最小1次,最多出现2次。并且还可以看到组1发生时该特定记录的发生。强制性应始终发生,输入数据可能会或可能不会出现可选项。但所有这些都需要被捕获......缺少什么

这是我的输入列数据。这是输入数据中唯一的列

1
2
3
1
2
4
5

根据输入数据中的数据规则表,是否有任何方法可以获得结果以确定哪些数据集丢失?就像在这个例子中一样,输出应该说第二次出现时第1组中缺少强制记录(3)。这只是可用的信息来自输入数据和数据规则表。

如果需要添加任何东西以获得所需的结果......我想听听它是什么。欢迎提出所有建议。

由于

1 个答案:

答案 0 :(得分:1)

我想你需要这样的东西:

with input as (select column_value id, 
                      count(1) over (partition by column_value order by null
                      rows between unbounded preceding and current row) cnt
                 from table(sys.odcinumberlist(1, 2, 3, 1, 2, 4, 5)))
select * 
  from data 
  where status = 'Mandatory' 
    and (id, occurence) not in (select id, cnt from input)

demo

  ID GRP        STATUS     OCCURENCE
---- ---------- ---------- ---------
   3 Group1     Mandatory          2

计算输入数据中id出现的次数,并将结果与​​数据中的必填项进行比较。

修改解释

select column_value id, 
       count(1) over (partition by column_value order by null
       rows between unbounded preceding and current row) cnt
  from table(sys.odcinumberlist(1, 2, 3, 1, 2, 4, 5))

此部分模拟您输入的数据。 table(sys.odcinumberlist(1, 2, 3, 1, 2, 4, 5))只是模拟输入,可能这些id在某些表中,从那里选择它们。对于每个提供的id,我在分析版本中使用函数count()来计算它的出现次数越来越多,所以我们有这个:

id    cnt
---   ---
  1     1
  1     2
  2     1
  2     2
  3     1
  4     1
  5     1

然后将这些对与数据中的强制对(id, occurence)进行比较。如果遗漏了某些内容,则select会在此行中显示not in个子句。

这就是我理解你的问题,也许你需要一些修改,但现在你有一些提示。希望这会有所帮助(对不起我的英语不好;-))。