在Oracle中,日期之间的列值发生变化时,选择行?

时间:2018-09-28 12:01:09

标签: oracle

Ex-我有如下表A的数据-

ID  Code    Date
1   A   19-Feb-18
1   B   18-Feb-18
1   B   17-Feb-18
1   B   16-Feb-18
2   A   17-Feb-18
2   B   16-Feb-18
3   A   17-Feb-18
3   A   16-Feb-18
4   D   16-Feb-18

现在我想要这样的结果:

情况1 :如果我需要16-feb-1819-feb-18之间的数据,则结果应如下所示

1   A   19-Feb-18
1   B   18-Feb-18
1   B   17-Feb-18
1   B   16-Feb-18
2   A   17-Feb-18
2   B   16-Feb-18

它将排除ID 3和4的记录,因为给定日期没有更改。

情况2 :如果我需要16-feb-1818-feb-18之间的数据,则结果应如下所示

2   A   17-Feb-18
2   B   16-Feb-18

由于给定日期没有更改,它将排除ID 1,3和4的记录。

2 个答案:

答案 0 :(得分:1)

您可以使用COUNT( DISTINCT code ) OVER ( PARTITION BY id )分析函数仅通过一次表扫描来获得答案:

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE table_name ( ID, Code, "DATE" ) AS
SELECT 1, 'A', DATE '2018-02-19' FROM DUAL UNION ALL
SELECT 1, 'B', DATE '2018-02-18' FROM DUAL UNION ALL
SELECT 1, 'B', DATE '2018-02-17' FROM DUAL UNION ALL
SELECT 1, 'B', DATE '2018-02-16' FROM DUAL UNION ALL
SELECT 2, 'A', DATE '2018-02-17' FROM DUAL UNION ALL
SELECT 2, 'B', DATE '2018-02-16' FROM DUAL UNION ALL
SELECT 3, 'A', DATE '2018-02-17' FROM DUAL UNION ALL
SELECT 3, 'A', DATE '2018-02-16' FROM DUAL UNION ALL
SELECT 4, 'D', DATE '2018-02-16' FROM DUAL;

查询1

SELECT ID,
       Code,
       "DATE"
FROM   (
  SELECT t.*,
         COUNT( DISTINCT code ) OVER ( PARTITION BY id ) AS num_changes
  FROM   table_name t
  WHERE  "DATE" BETWEEN DATE '2018-02-16'
                    AND DATE '2018-02-19'
)
WHERE num_changes > 1

Results

| ID | CODE |                 DATE |
|----|------|----------------------|
|  1 |    A | 2018-02-19T00:00:00Z |
|  1 |    B | 2018-02-18T00:00:00Z |
|  1 |    B | 2018-02-17T00:00:00Z |
|  1 |    B | 2018-02-16T00:00:00Z |
|  2 |    A | 2018-02-17T00:00:00Z |
|  2 |    B | 2018-02-16T00:00:00Z |

查询2

SELECT ID,
       Code,
       "DATE"
FROM   (
  SELECT t.*,
         COUNT( DISTINCT code ) OVER ( PARTITION BY id ) AS num_changes
  FROM   table_name t
  WHERE  "DATE" BETWEEN DATE '2018-02-16'
                    AND DATE '2018-02-18'
)
WHERE num_changes > 1

Results

| ID | CODE |                 DATE |
|----|------|----------------------|
|  2 |    A | 2018-02-17T00:00:00Z |
|  2 |    B | 2018-02-16T00:00:00Z |

答案 1 :(得分:0)

这是一个选项(您需要从第12行开始):

SQL> with test (id, code, datum) as
  2  (select 1, 'a', date '2018-02-19' from dual union all
  3   select 1, 'b', date '2018-02-18' from dual union all
  4   select 1, 'b', date '2018-02-17' from dual union all
  5   select 1, 'b', date '2018-02-16' from dual union all
  6   select 2, 'a', date '2018-02-17' from dual union all
  7   select 2, 'b', date '2018-02-16' from dual union all
  8   select 3, 'a', date '2018-02-17' from dual union all
  9   select 3, 'a', date '2018-02-16' from dual union all
 10   select 4, 'd', date '2018-02-16' from dual
 11  )
 12  select id, code, datum
 13  from test
 14  where id in (select id
 15               From test
 16               where datum between date '2018-02-16' and date '2018-02-18'
 17               group by id
 18               having count(distinct code) > 1)
 19  order by id, datum desc;

        ID C DATUM
---------- - ----------
         2 a 17.02.2018
         2 b 16.02.2018

SQL>