我有兩個表在MS Access 2010:加入兩個表,並顯示不同的記錄
PART註銷表:
PART_ID DRAWN_DATE LOCATION_ID
C0001 07/29/2013 501
C0002 07/29/2013 604
C0003 08/01/2013 703
C0004 08/01/2013 807
C0005 08/02/2013 505
C0006 08/02/2013 602
C0007 08/02/2013 707
C0008 08/03/2013 802
C0009 08/03/2013 803
C0001 10/01/2013 605
C0002 10/02/2013 704
C0004 10/05/2013 806
返還部件表:
PART_ID RETURN_DATE LOCATION_ID
C0001 09/04/2013 STORE
C0002 09/05/2013 STORE
C0004 09/10/2013 STORE
C0007 09/12/2013 STORE
C0008 09/13/2013 STORE
C0002 10/03/2013 STORE
這是我想要的結果:
PART_ID DRAWN_DATE LOG-OUT LOCATION RETURN_DATE RETURN LOCATION
C0001 07/29/2013 501 09/04/2013 STORE
C0001 10/01/2013 605
C0002 07/29/2013 604 09/05/2013 STORE
C0002 10/02/2013 704 10/03/2013 STORE
C0003 08/01/2013 703
C0004 08/01/2013 807 09/10/2013 STORE
C0004 10/05/2013 806
C0005 08/02/2013 505
C0006 08/02/2013 602
C0007 08/02/2013 707 09/13/2013 STORE
C0008 08/03/2013 802 10/03/2013 STORE
C0009 08/03/2013 803
B UT我只能得到這樣的:
PART_ID DRAWN_DATE LOG-OUT LOCATION RETURN_DATE RETURN LOCATION
C0001 07/29/2013 501 09/04/2013 STORE
C0001 10/01/2013 605 `09/04/2013 STORE`
C0002 07/29/2013 604 09/05/2013 STORE
`C0002 07/29/2013 604` 10/03/2013 STORE
C0002 10/02/2013 704 `09/05/2013 STORE`
`C0002 10/02/2013 704 10/03/2013 STORE`
C0003 08/01/2013 703
C0004 08/01/2013 807 09/10/2013 STORE
C0004 10/05/2013 806 `09/10/2013 STORE`
C0005 08/02/2013 505
C0006 08/02/2013 602
C0007 08/02/2013 707 09/13/2013 STORE
C0008 08/03/2013 802 10/03/2013 STORE
C0009 08/03/2013 803
我寫了後:
SELECT L.PART_ID, L.DRAWN_DATE, L.LOCATION_ID AS [LOG-OUT LOCATION], R.RETURN_DATE, R.LOCATION_ID AS RETURN_LOCATION FROM (SELECT * FROM [PART LOG-OUT] ORDER BY PART_ID) AS L LEFT JOIN (SELECT * FROM [PART RETURN] ORDER BY PART_ID) AS R ON L.PART_ID = R.PART_ID ORDER BY L.PART_ID, L.DRAWN_DATE, R.RETURN_DATE;
有人可以糾正我?謝謝!
是的,這就是我要找的。非常感謝! – user2652375
經過一些修改後,SELECT L.PART_ID,L.DRAWN_DATE,L.LOCATION_ID AS [LOG-OUT LOCATION], MIN(R.RETURN_DATE),MIN(R.LOCATION_ID)AS RETURN_LOCATION FROM(SELECT * FROM [PART LOG -OUT])AS L LEFT JOIN(SELECT * FROM [PART RETURN])AS R ON L.PART_ID = R.PART_ID AND L.DRAWN_DATE
user2652375