加入两个表并显示不同的记录

时间:2013-08-06 10:02:06

标签: ms-access join

我在MS Access 2010中有两个表:

PART LOG-OUT TABLE:

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 RETURN TABLE:

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

但我只能得到这个:

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;

有人可以纠正我吗?谢谢!

3 个答案:

答案 0 :(得分: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

请注意,由于没有任何内容可以将单次购买与单次退货配对(并且您的问题中没有这样的样本),因此逻辑非常适合配对。

答案 1 :(得分:0)

当您加入PART_ID列时,您获得的结果就是您期望获得的结果。

例如,C0001表中有两个LOG-OUT,这些C0001将加入PART RETURN表中的同一个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 ,因为没有什么可以排除行在彼此的返回表中:

{{1}}

您需要其他条件才能加入,或者您需要加入更多唯一标识符才能获得所需的结果。

答案 2 :(得分: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;