2013-08-06 74 views
-1

我有兩個表在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;

有人可以糾正我?謝謝!

回答

1

您的要求有點含糊,所以我可能會關閉,但您似乎想將返回與僅在返回之前的時間配對,像這樣?

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 [LOG_OUT LOCATION]) AS L 
LEFT JOIN (SELECT * FROM [PART_RETURN]) AS R 
    ON L.PART_ID = R.PART_ID AND L.DRAWN_DATE < R.RETURN_DATE 
GROUP BY L.LOCATION_ID,L.PART_ID,L.DRAWN_DATE 
ORDER BY L.PART_ID, L.DRAWN_DATE, MIN(R.RETURN_DATE) 

An SQLfiddle to test with

請注意,由於沒有將單次購物與單次退貨配對(並且在您的問題中沒有此類樣品),因此配對邏輯非常基本。

+0

是的,這就是我要找的。非常感謝! – user2652375

+0

經過一些修改後,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

0

當您加入PART_ID專欄時,您所得到的結果就是您期望得到的結果。

例如,有兩個C0001LOG-OUT表,這些都將加入到同一個C0001PART RETURN表作爲沒有什麼相互distringuish在返回表中的行:

PART LOG-OUT TABLE   -> PART RETURN TABLE 
C0001 07/29/2013  501 -> C0001 09/04/2013  STORE 
C0001 10/01/2013  605 -> C0001 09/04/2013  STORE 

您需要加入其他條件,或者您需要加入更多的唯一標識符才能實現您要查找的結果。

+0

有什麼建議嗎? – user2652375

+0

如果應該加入'C0001,07/29/2013,501'並且'C0001,10/01/2013,605'不應該,你需要找到一種方法來區分這兩者。根據你所顯示的表格結構,我看不到一種方法來實現它,因爲無法知道你應該在PART RETURN TABLE中加入哪個'C0001'。 –

0
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 OUTER 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; 
+0

程序運行後,LEFT OUTER JOIN將被轉換爲LEFT JOIN。 – user2652375

相關問題