SQL Oracle - 将连续行与过滤器组合在一起

时间:2015-06-20 19:33:49

标签: sql oracle oracle11g

| RecordId | foo_id | high_speed |   speed  | DateFrom   |  DateTo     |
------------------------------------------------------------------------
| 666542   |   12   |   60       |   10     | 09/11/2011 |  10/11/2011 |
| 666986   |   13   |   20       |   20     | 11/11/2011 |  11/11/2011 |
| 666996   |   12   |   0        |   0      | 13/11/2011 |  17/11/2011 |
| 755485   |   12   |   0        |   0      | 01/11/2011 |  14/11/2011 |
| 758545   |   12   |   70       |   50     | 15/11/2011 |  26/11/2011 |
| 796956   |   12   |   40       |   40     | 09/11/2011 |  09/11/2011 |
| 799656   |   13   |   25       |   20     | 09/11/2011 | 09/11/2011  |
| 808845   |   12   |   0        |   0      | 15/11/2011 | 15/11/2011  |
| 823323   |   12   |   0        |   0      | 15/11/2011 | 16/11/2011  |
| 823669   |   12   |   0        |   0      | 17/11/2011 | 18/11/2011  |
| 899555   |   12   |   0        |   0      | 18/11/2011 | 19/11/2011  |
| 990990   |   12   |   20       |   10     | 12/11/2011 | 12/11/2011  |

这里,我想构建数据库视图,它组合了速度= 0的连续行。在这种情况下,DateFrom将是第一行和第一行的DateFrom值。 DateTo将是最后一行的DateTo值。表格结果如下:

| foo_id | high_speed |    speed  | DateFrom    |    DateTo    |
---------------------------------------------------
|   12   |  60        |     10    |  09/11/2011 |  10/11/2011  |
|   13   |  20        |     20    |  11/11/2011 |  11/11/2011  |
|   12   |  0         |     0     |  13/11/2011 |  14/11/2011  |
|   12   |  70        |     50    |  15/11/2011 |  26/11/2011  |
|   12   |  40        |     40    |  09/11/2011 |  09/11/2011  |
|   13   |  25        |     20    |  09/11/2011 |  09/11/2011  |
|   12   |  0         |     0     |  15/11/2011 |  19/11/2011  |
|   12   |  20        |     10    |  12/11/2011 |  12/11/2011  |

为了获得结合了速度= 0的连续行的结果,我设计了用户sql查询的视图如下:

select foo_id, high_speed, speed, datefrom, dateto, dateto-datefrom period
  from (
    select recordid, foo_id, high_speed, speed, datefrom, 
      case when tmp = 2 then lead(dateto) over (order by recordid) 
                        else dateto end dateto, tmp 
      from (
        select test.*, case when speed <> 0 then 1 
                       when lag(speed) over (order by recordid) <> 0 then 2
                       when lead(speed) over (order by recordid) <> 0 then 3 
                       end tmp
          from test )
      where tmp is not null)
   where tmp in (1, 2) order by recordid

现在,我必须应用与foo_id相同的结果。要获得此结果,需要将foo_id的过滤器应用于内部嵌套查询。是否可以使用参数创建视图?或者我如何设计任何带有一个参数的函数,该函数将与foo_id列进行比较。前 -

select foo_id, high_speed, speed, datefrom, dateto, dateto-datefrom period
      from (
        select recordid, foo_id, high_speed, speed, datefrom, 
          case when tmp = 2 then lead(dateto) over (order by recordid) 
                            else dateto end dateto, tmp 
          from (
            select test.*, case when speed <> 0 then 1 
                           when lag(speed) over (order by recordid) <> 0 then 2
                           when lead(speed) over (order by recordid) <> 0 then 3 
                           end tmp
              from test where foo_id=12 )
          where tmp is not null)
       where tmp in (1, 2) order by recordid

1 个答案:

答案 0 :(得分:1)

问了这个问题已经过去了四年。但是Oracle在Oracle 12c中添加了MATCH_RECOGNIZE子句,这使解决方案更加简单。

SELECT
  foo_id, high_speed, speed,
  NVL(DateFromZ, DateFrom) DateFrom,
  NVL(DateToZ, DateTo) DateTo
FROM test
MATCH_RECOGNIZE (
  ORDER BY RecordId
  MEASURES
    FIRST(zeros.DateFrom) AS DateFromZ,
    FINAL LAST(zeros.DateTo) AS DateToZ,
    COUNT(*) AS cnt
  ALL ROWS PER MATCH WITH UNMATCHED ROWS
  PATTERN (zeros+)
  DEFINE
    zeros AS zeros.speed = 0
)
WHERE speed > 0 OR cnt = 1
ORDER BY RecordId;

输出:

+--------+------------+-------+------------+------------+
| FOO_ID | HIGH_SPEED | SPEED |  DATEFROM  |   DATETO   |
+--------+------------+-------+------------+------------+
|     12 |         60 |    10 | 09/11/2011 | 10/11/2011 |
|     13 |         20 |    20 | 11/11/2011 | 11/11/2011 |
|     12 |          0 |     0 | 13/11/2011 | 14/11/2011 |
|     12 |         70 |    50 | 15/11/2011 | 26/11/2011 |
|     12 |         40 |    40 | 09/11/2011 | 09/11/2011 |
|     13 |         25 |    20 | 09/11/2011 | 09/11/2011 |
|     12 |          0 |     0 | 15/11/2011 | 19/11/2011 |
|     12 |         20 |    10 | 12/11/2011 | 12/11/2011 |
+--------+------------+-------+------------+------------+

db<>fiddle上演示。