我搜索了查询以找到重复的记录,但是我的要求略有不同,我需要找到从一个部门移动到另一个部门的记录。下面是主要的表公司,其下面有预期的产量。很抱歉没有为这篇文章找到合适的格式。
ID PKTNUM DEPT MONTH
============================
1 10 acc Jan
2 20 sales Feb
3 10 market Mar
4 30 admin Apr
5 20 HR May
6 10 finance Feb
7 20 sales Jun
8 30 acc Jun
9 20 acc Aug
10 30 finance Sep
Expected output:
================
[Moved from other dept to ACC]
ID PKTNUM DEPT MONTH
============================
4 30 admin Apr
7 20 sales Jun
[Moved from other dept to Sales]
ID PKTNUM DEPT MONTH
============================
5 20 HR May
[Moved from other dept to Finance]
ID PKTNUM DEPT MONTH
============================
3 10 market Mar
8 30 acc Jun
Steps followed to create the table.
----------------------------------
CREATE TABLE COMPANY( ID INT PRIMARY KEY NOT NULL, PKTNUM INT NOT NULL, DEPT TEXT NOT NULL, MONTH TEXT);
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (1, 10, 'acc', 'JAN' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (2, 20, 'sales', 'FEB' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (3, 10, 'market', 'MAR' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (4, 30, 'admin', 'APR' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (5, 20, 'HR', 'MAY' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (6, 10, 'finance', 'FEB' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (7, 20, 'sales', 'JUN' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (8, 30, 'acc', 'JUN' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (9, 20, 'acc', 'AUG' );
INSERT INTO COMPANY (ID,PKTNUM,DEPT,MONTH) VALUES (10,30, 'finance', 'SEP' );
这是我试过的,非常接近我的预期结果,但还不太好。
sqlite> select * from company a join ( select * from company as a where dept = 'acc') b on a.pktNum = b.pktNum and a.dept != b.dept;
ID PKTNUM DEPT MONTH ID PKTNUM DEPT MONTH
---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------
3 10 market MAR 1 10 acc JAN
6 10 finance FEB 1 10 acc JAN
4 30 admin APR 8 30 acc JUN
10 30 finance SEP 8 30 acc JUN
2 20 sales FEB 9 20 acc AUG
5 20 HR MAY 9 20 acc AUG
7 20 sales JUN 9 20 acc AUG