您可以使用分析LAG()函數。
設置
CREATE TABLE t
(
Employee_id VARCHAR2(1),
Status VARCHAR2(1),
email VARCHAR2(10),
partition INT
);
INSERT ALL
INTO t (Employee_id, Status, email, partition)
VALUES ('A', 'P', '[email protected]', 1)
INTO t (Employee_id, Status, email, partition)
VALUES ('A', 'P', '[email protected]', 2)
INTO t (Employee_id, Status, email, partition)
VALUES ('D', 'T', '[email protected]', 1)
INTO t (Employee_id, Status, email, partition)
VALUES ('D', 'T', '[email protected]', 2)
INTO t (Employee_id, Status, email, partition)
VALUES ('G', 'P', '[email protected]', 1)
INTO t (Employee_id, Status, email, partition)
VALUES ('G', 'T', '[email protected]', 2)
SELECT * FROM dual;
COMMIT;
查詢
SQL> WITH t1 AS(
2 SELECT t.*, LAG(status) OVER(PARTITION BY employee_id, email ORDER BY status) rn FROM t
3 ),
4 t2 AS(
5 SELECT Employee_id, Status, email, PARTITION FROM t1
6 WHERE
7 status <> rn
8 )
9 SELECT t.Employee_id,
10 t.Status,
11 t.email,
12 t.partition
13 FROM t,
14 t2
15 WHERE t.Employee_id = t2.Employee_id
16 ORDER BY t.partition;
EMPLOYEE_ID STATUS EMAIL PARTITION
----------- ------ ---------- ----------
G P [email protected] 1
G T [email protected] 2
SQL>
顯示您想要的輸出。 –
@NareshK你可以使用分析** LAG()**。 –
你需要這個查詢,因爲你已經非規範化搞砸了數據,承認它;) – Drew