sql >> Database teknologi >  >> RDS >> Oracle

Vælg rækker, når kolonneværdien ændres mellem dato i Oracle?

Du kan bruge COUNT( DISTINCT code ) OVER ( PARTITION BY id ) analytisk funktion for at få dit svar ved hjælp af kun én tabelscanning:

SQL Fiddle

Oracle 11g R2 Schema Setup :

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;
 

Forespørgsel 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
 

Resultater :

| 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 |

Forespørgsel 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
 

Resultater :

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

  1. SELECT AS-kolonnen findes ikke i WHERE-udtrykket

  2. Spørg de to byer i STATION med de korteste og længste CITY-navne,

  3. Sorterbare UUID'er og tilsidesættende ActiveRecord::Base

  4. Masseindlæg af MySQL-relaterede tabeller fra bash