简体   繁体   English

MS Access - 通过多个连接表求和的查询

[英]MS Access - query that sums a sum through multiple joined tables

I have three tables Models , Buildups , and Components with many-to-many join tables in between them.我有三个表ModelsBuildupsComponents ,它们之间有多对多连接表。 Each model can have multiple buildups and buildups are composed of multiple components.每个模型可以有多个构建,并且构建由多个组件组成。 The components table has a field called Retail .组件表有一个名为Retail的字段。

I'm trying to create a query for a report where the user can see a model and know the total buildup retail amount which would be the sum of the Retail field of each component in the buildup and then a sum of each buildup in the model.我正在尝试为报告创建一个查询,用户可以在其中查看模型并知道总累积零售量,这将是累积中每个组件的Retail字段的总和,然后是模型中每个累积的总和.

I need a way to reference the Sum of the Sum of components without the enter parameter box appearing when the query is run (strange enough, when the parameter box is left blank it calculates correctly but I don't want the box to pop up).我需要一种在查询运行时不出现输入参数框的情况下引用组件总和的方法(很奇怪,当参数框留空时,它计算正确,但我不希望该框弹出) .

Is the solution a nested query?解决方案是嵌套查询吗? If so, how would I do that?如果是这样,我该怎么做? Or is the solution to use DSum() ?或者是使用DSum()的解决方案? Once again, if so, how would I implement that?再一次,如果是这样,我将如何实施?

I'm not sure what to reference to make the criteria portion of the DSum() formula work correctly.我不确定要参考什么才能使DSum()公式的标准部分正常工作。

Unless I've misunderstood your database structure or what you are looking to obtain, it seems like this would be sufficient:除非我误解了您的数据库结构或您希望获得的内容,否则这似乎就足够了:

select 
    mo.JandelModelID,
    sum(co.retail) as Total_Retail
from
    (   
        (
            tblJandelModels mo inner join tblJandelModelBuildups mb on
            mo.JandelModelID = mb.JandelModelID
        ) 
        inner join tblBuildupComponents bc on mb.BuildupID = bc.BuildupID
    )
    inner join tblComponents co on bc.ComponentID = co.ComponentID
group by
    mo.JandelModelID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM