我对子查询并不擅长。我试图通过从另一个表中选择某些值来创建列,然后将这些列插入到我的表中[Presummary 7/24。 。 。上午]。事情在我的选择子查询中生成我想要插入的值,我引用我的表的工作站列[Presummary 7/24。 。 。上午]。我认为Access SQL说我不允许这样做,但我不确定如何绕过它。
这是我的查询(由我写的VBA函数生成):
INSERT INTO [Presummary 7/24/2014 11:07:33 AM] (660201, 660202, 660203, 660206, 660207, 660208, 660209)
VALUES (
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660201' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION,
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660202' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION,
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660203' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION,
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660206' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION,
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660207' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION,
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660208' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION,
SELECT h.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660209' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h.WORKSTATION
)
当我尝试运行此代码时,它说 “查询表达式中的语法错误'SELECT h.sumOFHRS_CLA ... workstation = h.WORKSTATION”。 (它只引用第一个子查询,但我确信所有子查询中都适用相同的错误。)
以下内容也不起作用:
INSERT INTO [Presummary 7/24/2014 11:07:33 AM] (660201, 660202, 660203, 660206, 660207, 660208, 660209)
VALUES (
SELECT h1.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h1,
SELECT h2.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h2,
SELECT h3.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h3,
SELECT h4.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h4,
SELECT h5.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h5,
SELECT h6.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h6,
SELECT h7.SumOFHRS_Claimed FROM [qry EngineHoursSummaryA] as h7
)
WHERE Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660201' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h1.WORKSTATION
AND Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660202' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h2.WORKSTATION
AND Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660203' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h3.WORKSTATION
AND Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660206' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h4.WORKSTATION
AND Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660207' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h5.WORKSTATION
AND Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660208' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h6.WORKSTATION
AND Right(h.MODULE_ENGINE_SWO_SERIAL,6) = '660209' AND [Presummary 7/24/2014 11:01:44 AM].workstation = h7.WORKSTATION
如何从查询中获得我想要的结果?
答案 0 :(得分:0)
尝试使用INSERT INTO tablename (column1, column2...) SELECT column1, column2.. FROM tablename1 WHERE conditions
答案 1 :(得分:0)
试试这段代码,
INSERT INTO [Presummary 7/24/2014 11:07:33 AM] (660201, 660202, 660203, 660206, 660207, 660208, 660209)
SELECT Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660201', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E1,
Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660202', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E2,
Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660203', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E3,
Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660206', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E4,
Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660207', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E5,
Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660208', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E6,
Sum(IIF(Right([qry EngineHoursSummaryA].MODULE_ENGINE_SWO_SERIAL,6) = '660209', [qry EngineHoursSummaryA].SumOFHRS_Claimed, 0)) As E7
FROM [Presummary 7/24/2014 11:07:33 AM] INNER JOIN [qry EngineHoursSummaryA] ON [Presummary 7/24/2014 11:01:44 AM].workstation = [qry EngineHoursSummaryA].WORKSTATION;
答案 2 :(得分:0)
从未弄清楚语法错误的真正原因。但无论如何,对于我想要做的事情,我意识到我需要使用UPDATE而不是INSERT语句。