我在Microsoft Access 2013中有两个表:
ID PART_ID SERV_DATE SERV_REMARK 1 A0001 11/1/2013 GOOD#1 2 A0001 11/13/2013 GOOD#2 3 A0001 11/25/2013 GOOD#3 4 B0001 11/26/2013 GOOD#4 5 C0001 12/1/2013 GOOD#5 6 C0001 12/10/2013 GOOD#6 7 C0001 12/20/2013 GOOD#7
ID PART_ID DRAWN_DATE DRAWN_REASON DRAWN_TO 1 A0001 11/6/2013 PM OW601 2 A0001 11/20/2013 120 PM OW603 3 A0001 11/30/2013 REPLACEMENT OW605 4 C0001 12/2/2013 30 PM OW701 5 C0001 12/15/2013 180 PM OW702
我想加入这两个表,得到如下所示的结果:
PART_ID SERV_DATE SERV_REMARK DRAWN_DATE DRAWN_REASON DRAWN_TO A0001 11/1/2013 GOOD#1 11/6/2013 PM OW601 A0001 11/13/2013 GOOD#2 11/20/2013 120 PM OW603 A0001 11/25/2013 GOOD#3 11/30/2013 REPLACEMENT OW605 B0001 11/26/2013 GOOD#4 C0001 12/1/2013 GOOD#5 12/2/2013 PM OW701 C0001 12/10/2013 GOOD#6 12/15/2013 180 PM OW702 C0001 12/20/2013 GOOD#7
我一直 working for weeks ,但仍然无法获得结果。 有人可以给我看看查询吗?非常感谢!
我试过了:
SELECT s.PART_ID,s.SERV_DATE,s.SERV_REMARK,
w.DRAWN_DATE,w.DRAWN_REASON,w.DRAWN_TO
来自tblService AS的 LEFT JOIN tblWithdraw AS w
ON s.PART_ID = w.PART_ID
结果是:
PART_ID SERV_DATE SERV_REMARK DRAWN_DATE DRAWN_REASON DRAWN_TO A0001 11/1/2013 GOOD #1 11/6/2013 PM OW601 A0001 11/1/2013 GOOD #1 11/20/2013 120 PM OW602 A0001 11/1/2013 GOOD #1 11/30/2013 REPLACEMENT OW603 A0001 11/13/2013 GOOD #2 11/6/2013 PM OW601 A0001 11/13/2013 GOOD #2 11/20/2013 120 PM OW602 A0001 11/13/2013 GOOD #2 11/30/2013 REPLACEMENT OW603 A0001 11/25/2013 GOOD #3 11/6/2013 PM OW601 A0001 11/25/2013 GOOD #3 11/20/2013 120 PM OW602 A0001 11/25/2013 GOOD #3 11/30/2013 REPLACEMENT OW603 B0001 11/26/2013 GOOD #4 C0001 12/1/2013 GOOD #5 12/2/2013 PM OW701 C0001 12/1/2013 GOOD #5 12/15/2013 180 PM OW702 C0001 12/10/2013 GOOD #6 12/2/2013 PM OW701 C0001 12/10/2013 GOOD #6 12/15/2013 180 PM OW702 C0001 12/20/2013 GOOD #7 12/2/2013 PM OW701 C0001 12/20/2013 GOOD #7 12/15/2013 180 PM OW702
答案 0 :(得分:3)
使用相关子查询为DRAWN_DATE
和PART_ID
的每个组合获取下一个SERV_DATE
。在Access 2007中使用示例数据,这是以下查询的输出。
PART_ID SERV_DATE SERV_REMARK next_drawn_date
A0001 11/1/2013 GOOD#1 11/6/2013
A0001 11/13/2013 GOOD#2 11/20/2013
A0001 11/25/2013 GOOD#3 11/30/2013
B0001 11/26/2013 GOOD#4
C0001 12/1/2013 GOOD#5 12/2/2013
C0001 12/10/2013 GOOD#6 12/15/2013
C0001 12/20/2013 GOOD#7
SELECT
s.PART_ID,
s.SERV_DATE,
s.SERV_REMARK,
(
SELECT Min(DRAWN_DATE)
FROM tblWithDraw
WHERE
PART_ID = s.PART_ID
AND DRAWN_DATE >= s.SERV_DATE
) AS next_drawn_date
FROM tblService AS s;
然后您可以将其用作子查询并将其加入tblWithdraw
。
SELECT
sub.PART_ID,
sub.SERV_DATE,
sub.SERV_REMARK,
sub.next_drawn_date AS DRAWN_DATE,
w.DRAWN_REASON,
w.DRAWN_TO
FROM
(
SELECT
s.PART_ID,
s.SERV_DATE,
s.SERV_REMARK,
(
SELECT Min(DRAWN_DATE)
FROM tblWithDraw
WHERE
PART_ID = s.PART_ID
AND DRAWN_DATE >= s.SERV_DATE
) AS next_drawn_date
FROM tblService AS s
) AS sub
LEFT JOIN tblWithdraw AS w
ON
(sub.PART_ID = w.PART_ID)
AND (sub.next_drawn_date = w.DRAWN_DATE);
答案 1 :(得分:1)
我原本认为以下内容适合您:
SELECT s.PART_ID, s.SERV_DATE, s.SERV_REMARK,
w.DRAWN_DATE, w.DRAWN_REASON, w.DRAWN_TO
FROM tblService AS s
LEFT JOIN tblWithdraw AS w
ON s.PART_ID = w.PART_ID
然而,这不起作用,因为部件ID在tblService
表中不是唯一的。然后我开始寻找两个表中匹配的日期字段。日期字段不匹配。
因此,根据您在此处显示的内容,您无法将两个表链接在一起。
您的输出看起来像是想要遍历tblService
表,然后在tblWithdraw
表中找到匹配的记录并将它们匹配在一起。然后,如果PART_ID
表中存在具有相同tblService
的第二条记录,请将其与PART_ID
表中第二次出现的相同tblWithdraw
匹配。然后多次重复此操作。如果tblWithdraw
中的匹配记录与n
中的tblService
匹配匹配,则无论如何都会输出tblService
中找到的信息。
您无法在SQL中执行这样的复杂查询。 如果您真的想这样做,可以使用VBA,C#和/或等代码来迭代tblService
然后遍历tblWithdraw
以查找您要查找的内容。
更好的选择:如果您显示的表中只包含您要显示的字段,则需要修改表并向tblService
添加唯一的主键字段。然后在tblWithdraw
表中添加一个与tblService
主键匹配的外键。
然后以下内容将起作用:
SELECT s.PART_ID, s.SERV_DATE, s.SERV_REMARK,
w.DRAWN_DATE, w.DRAWN_REASON, w.DRAWN_TO
FROM tblService AS s
LEFT JOIN tblWithdraw AS w
ON s.tblServiceID = w.tblServiceID
答案 2 :(得分:0)
SELECT s.PART_ID,s.SERV_DATE,s.SERV_REMARK, w.DRAWN_DATE,w.DRAWN_REASON,w.DRAWN_TO 来自tblService AS LEFT JOIN tblWithdraw AS w ON s.tblServiceID = w。“TWLWITHDRAW” - 小修正