将多个ms访问查询组合到一个现有表中

时间:2016-10-19 09:19:52

标签: ms-access access-vba

我对MS Access查询知之甚少感到困惑。我有2张桌子。

1. Table Output and
2. Dump : this is a linked table to an excel file

我使用了http://allenbrowne.com/func-concat.html

中的Concatenate用户功能

我总共有8个不同的查询,如下所示:

Query_1 : DELETE table FROM [Output];

Query_2 : INSERT INTO [Output] (SERVICE_REQUEST_NO) SELECT DISTINCT SERVICE_REQUEST_NO
FROM Dump
ORDER BY SERVICE_REQUEST_NO;

Query_3 : SELECT DISTINCT i.SERVICE_REQUEST_NO, ConcatRelated(
"EMERTEL_NO",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") AS EMERTEL_NOss
FROM Dump AS i;

Query_4 : SELECT DISTINCT i.SERVICE_REQUEST_NO, ConcatRelated(
"LOGGED_DATE",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") AS LOGGED_DATEss
FROM Dump AS i;

Query_5 : SELECT DISTINCT i.SERVICE_REQUEST_NO, ConcatRelated(
"PART_NO",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") AS PART_NOss
FROM Dump AS i;

Query_6 : SELECT DISTINCT i.SERVICE_REQUEST_NO, ConcatRelated(
"PART_DESC",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") AS PART_DESCss
FROM Dump AS i;

Query_7 : SELECT DISTINCT i.SERVICE_REQUEST_NO, ConcatRelated(
"STATUS_DESC",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") AS STATUS_DESCss
FROM Dump AS i;

Query_8 : SELECT DISTINCT i.SERVICE_REQUEST_NO, ConcatRelated(
"STATUS_DATE",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") AS STATUS_DATEss
FROM Dump AS i;

我的问题是我想更新或插入所有这些查询的输出到表"输出"匹配"SERVICE_REQUEST_NO"字段

如果您想查看我的MS Access数据库,请告诉我。

请帮帮我。

感谢您的支持Table Output fields enter image description here

1 个答案:

答案 0 :(得分:0)

首先,您应该将其合并为一个查询。每个查询的方式只选择Dump表中的一列。如果您将查询分开,则每个查询都必须JOIN,这很痛苦并且会产生更多错误机会。作为一个查询,它看起来像:

INSERT INTO [Output] 
(
    SERVICE_REQUEST_NO,
    EMERTEL_NOss,
    LOGGED_DATEss,
    PART_NOss,
    PART_DESCss,
    STATUS_DESCss,
    STATUS_DATEss
) 
SELECT DISTINCT
SERVICE_REQUEST_NO,
Concatrelated(
"EMERTEL_NO",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") 
    AS EMERTEL_NOss,
Concatrelated(
"LOGGED_DATE",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") 
    AS LOGGED_DATEss,
Concatrelated(
"PART_NO",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") 
    AS PART_NOss,
Concatrelated(
"PART_DESC",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") 
    AS PART_DESCss,
Concatrelated(
"STATUS_DESC",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") 
    AS STATUS_DESCss,
Concatrelated(
"STATUS_DATE",
"Dump",
"SERVICE_REQUEST_NO = """ & [SERVICE_REQUEST_NO] & """") 
    AS STATUS_DATEss
FROM [Dump]

我不确定为什么您在不查看实际数据的情况下在每个查询中使用DISTINCT。出于本答案的目的,我假设您正在使用它来避免[Output中完全重复的行。我的选择列表顶部的单DISTINCT负责处理。