两个选定表或查询中的列数与

时间:2016-03-03 05:01:08

标签: sql-server ms-access access-vba ms-access-2010 ms-access-2007

这是我的SQL。

选择     [Ledger Entries JC]。[目标代码],     DLookUp(“描述”,“ACTVTY和CMDTY描述”,“L4 ='”& [对象代码]&“'”)AS [对象代码描述],     左([Ledger Entries JC]![子公司],2)AS L4,     DLookUp(“描述”,“ACTVTY和CMDTY描述”,“L4 ='”& [L4]&“'”)AS [L4描述],     左([Ledger Entries JC]![子公司],3)AS L5,     DLookUp(“描述”,“ACTVTY和CMDTY描述”,“L4 ='”& [L5]&“'”)AS [L5描述],     左([Ledger Entries JC]![子公司],4)AS L6,     DLookUp(“描述”,“ACTVTY和CMDTY描述”,“L4 ='”& [L6]&“'”)AS [L6描述],     对([Ledger Entries JC]![子公司],4)AS L7,     DLookUp(“描述”,“ACTVTY和CMDTY描述”,“L4 ='”& [L7]&“'”)AS [L7描述],     [Ledger Entries JC] .Amount,     DLookUp(“预算”,“商品活动预算”,“WBS ='”& [WBS]&“'”)AS预算,     [L6]& “” &安培; [对象代码]& “” &安培; [L7]作为WBS,     [Ledger Entries JC]。日期 从     [Ledger Entries JC] 联盟     转变     总和([商品活动预算]。预算)作为[预算总额] 选择     [商品活动预算] .L4,     [商品活动预算]。[L4说明],     [商品活动预算]。预算AS [总预算],     [商品活动预算] .L5,     [商品活动预算]。[L5说明],     [商品活动预算] .L6,     [商品活动预算]。[L6说明],     [商品活动预算] .L7,     [商品活动预算] .WBS AS WBS,     [商品活动预算]。[WBS描述],     [Ledger Entries JC Qry] .WBS AS [WBS 2],     总和([Ledger Entries JC Qry] .Amount)作为金额 从     [Ledger Entries JC Qry]         正确加入[商品活动预算]         ON [Ledger Entries JC Qry] .WBS = [按商品划分的活动预算] .WBS 通过...分组     [商品活动预算] .L4,     [商品活动预算]。[L4说明],     [商品活动预算]。预算,     [商品活动预算] .L5,     [商品活动预算]。[L5说明],     [商品活动预算] .L6,     [商品活动预算]。[L6说明],     [商品活动预算] .L7,     [商品活动预算] .WBS,     [商品活动预算]。[WBS描述],     [Ledger Entries JC Qry] .WBS 枢     [商品活动预算]。[目标代码] In(57523,57531,57532,57533,57535,57537,57540,57590,57626,57627,57628,57629,57630,58230)

1 个答案:

答案 0 :(得分:0)

您有UNION查询,此类查询中的所有选择应具有相同数量的具有相同数据类型的字段。在这里,第一个查询中有14个字段,第二个查询中有12个字段,这会产生错误。此外,您还需要检查每列的数据类型,它们应在两个查询中对应。这是您的格式化查询,请编辑问题,我不能。

SELECT
    [Ledger Entries JC].[Object Code],
    DLookUp("Description", "ACTVTY and CMDTY Descriptions", "L4='" & [Object Code] & "'") AS [Object Code Description],
    Left([Ledger Entries JC]![Subsidiary], 2) AS L4,
    DLookUp("Description", "ACTVTY and CMDTY Descriptions", "L4='" & [L4] & "'") AS [L4 Description],
    Left([Ledger Entries JC]![Subsidiary], 3) AS L5,
    DLookUp("Description", "ACTVTY and CMDTY Descriptions", "L4='" & [L5] & "'") AS [L5 Description],
    Left([Ledger Entries JC]![Subsidiary], 4) AS L6,
    DLookUp("Description", "ACTVTY and CMDTY Descriptions", "L4='" & [L6] & "'") AS [L6 Description],
    Right([Ledger Entries JC]![Subsidiary], 4) AS L7,
    DLookUp("Description", "ACTVTY and CMDTY Descriptions", "L4='" & [L7] & "'") AS [L7 Description],
    [Ledger Entries JC].Amount,
    DLookUp("Budget", "Budget By Activity By Commodity", "WBS='" & [WBS] & "'") AS Budget,
    [L6] & "." & [Object Code] & "." & [L7] AS WBS,
    [Ledger Entries JC].Date
FROM
    [Ledger Entries JC]
UNION
    Transform
    Sum([Budget By Activity By Commodity].Budget) As [Total Of Budget]
SELECT
    [Budget By Activity By Commodity].L4,
    [Budget By Activity By Commodity].[L4 Description],
    [Budget By Activity By Commodity].Budget AS [Total Budget],
    [Budget By Activity By Commodity].L5,
    [Budget By Activity By Commodity].[L5 Description],
    [Budget By Activity By Commodity].L6,
    [Budget By Activity By Commodity].[L6 Description],
    [Budget By Activity By Commodity].L7,
    [Budget By Activity By Commodity].WBS AS WBS,
    [Budget By Activity By Commodity].[WBS Description],
    [Ledger Entries JC Qry].WBS AS [WBS 2],
    Sum([Ledger Entries JC Qry].Amount) As Amount
FROM
    [Ledger Entries JC Qry]
        RIGHT JOIN [Budget By Activity By Commodity]
        ON [Ledger Entries JC Qry].WBS = [Budget By Activity By Commodity].WBS
Group BY
    [Budget By Activity By Commodity].L4,
    [Budget By Activity By Commodity].[L4 Description],
    [Budget By Activity By Commodity].Budget,
    [Budget By Activity By Commodity].L5,
    [Budget By Activity By Commodity].[L5 Description],
    [Budget By Activity By Commodity].L6,
    [Budget By Activity By Commodity].[L6 Description],
    [Budget By Activity By Commodity].L7,
    [Budget By Activity By Commodity].WBS,
    [Budget By Activity By Commodity].[WBS Description],
    [Ledger Entries JC Qry].WBS
PIVOT
    [Budget By Activity By Commodity].[Object Code] In(57523, 57531, 57532, 57533, 57535, 57537, 57540, 57590, 57626, 57627, 57628, 57629, 57630, 58230)