0

当我运行下面的代码时,我期望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 |
4

1 回答 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 |
于 2020-05-01T13:45:17.650 回答