当使用MATCH_RECOGNIZE时,FINAL SUM的NULLIF加倍。

问题描述 投票:0回答:1

当我运行下面的代码时,我希望得到的是 b1b2 要平等,但。b2 是双倍的。我是不是做错了什么?这是数据库的一个错误吗?我们正在运行 Oracle 12c (12.2.0.1.0).

WITH TBL AS
  (
  SELECT 1 a, 1 b FROM DUAL UNION ALL
  SELECT 1 a, 2 b FROM DUAL UNION ALL
  SELECT 1 a, 3 b FROM DUAL UNION ALL
  SELECT 1 a, 4 b FROM DUAL
  )
SELECT
  *
FROM
  TBL
MATCH_RECOGNIZE
  (
  PARTITION BY
    a
  ORDER BY
    b
  MEASURES
    FINAL SUM(b) b1,
    NULLIF(FINAL SUM(b), 0) b2
  ALL ROWS PER MATCH WITH UNMATCHED ROWS
  AFTER MATCH SKIP PAST LAST ROW
  PATTERN
    (C*)
  DEFINE
    C AS B > 0
  ) mr

结果。

| A | B | B1 | B2 |
|---|---|----|----|
| 1 | 1 | 10 | 20 |
| 1 | 2 | 10 | 20 |
| 1 | 3 | 10 | 20 |
| 1 | 4 | 10 | 20 |
oracle oracle12c nullif match-recognize
1个回答
0
投票

问题似乎出在 NULLIF 当我把同样的东西转换为它的逻辑等价物时,它工作得很好。CASE WHEN expr1 = expr 2 THEN NULL ELSE expr1 END

WITH TBL AS
      (
      SELECT 1 a, 1 b FROM DUAL UNION ALL
      SELECT 1 a, 2 b FROM DUAL UNION ALL
      SELECT 1 a, 3 b FROM DUAL UNION ALL
      SELECT 1 a, 4 b FROM DUAL
      )
    SELECT
      *
    FROM
      TBL
    MATCH_RECOGNIZE
      (
      PARTITION BY
        a
      ORDER BY
        b
      MEASURES
        FINAL SUM(b) b1,
       CASE WHEN FINAL SUM(b)=0 THEN NULL ELSE FINAL SUM(b) END b2
      ALL ROWS PER MATCH WITH UNMATCHED ROWS
      AFTER MATCH SKIP PAST LAST ROW
      PATTERN
        (C*)
      DEFINE
        C AS B > 0
      ) mr 

结果

    | A | B | B1 | B2 |
    |---|---|----|----|
    | 1 | 1 | 10 | 10 |
    | 1 | 2 | 10 | 10 |
    | 1 | 3 | 10 | 10 |
    | 1 | 4 | 10 | 10 |
© www.soinside.com 2019 - 2024. All rights reserved.