-1

我在 MS Access 2010 中有两个表:

部分登出表:

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

但我只能得到这个:

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;

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

4

3 回答 3

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)

一个用于测试的 SQLfiddle

请注意,由于没有将单次购买与单次退货配对(并且您的问题中没有此类示例),因此将它们配对的逻辑非常基本。

于 2013-08-06T10:24:53.820 回答
0

当您加入该PART_ID专栏时,您得到的结果就是您期望得到的结果。

例如,表中有两个C0001LOG-OUT它们将连接到表中的同一C0001PART 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

您需要另一个标准来加入,或者您需要加入一些更独特的标识符才能获得您正在寻找的结果。

于 2013-08-06T10:10:20.147 回答
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;
于 2013-08-06T10:11:17.887 回答