可以在同一个查询中对计算字段求和吗?
PARAMETERS [First Date] DateTime, [Second Date] DateTime, [Shift] Text ( 255 );
SELECT
Main.[Mth/day],
Main.Shift,
([Run Hrs]*[Actual Lbs/hr]) AS [Gross Production - LBS],
[Scrap Lbs 1]+[Scrap Lbs 2]+[Scrap Lbs 3] AS [Scrap Produced - LBS],
[Run Hrs]*[Actual Lbs/hr]-([Scrap Lbs 1]+[Scrap Lbs 2]+[Scrap Lbs 3]) AS [Net Production],
[Run Hrs]*[Actual Lbs/hr]*0.035 AS [Scrap Target],
[Run Hrs]*[Std lbs/hr]-([Run Hrs]*[Actual Lbs/hr])*0.035 AS [Target Net Production],
[Run Hrs]*[Std lbs/hr] AS [Target Gross Production],
(([Run Hrs]*[Std Lbs/hr])/([Run Hrs]*[Actual Lbs/hr]*0.035)) AS [Target Scrap Rate]
FROM Main
WHERE
(((Main.[Mth/day]) Between [First Date] And [Second Date]
And [Shift]=Main.Shift
And [Std lbs/hr]>0));
我还需要对每个计算字段进行求和,以便他们还可以检索每月摘要等。我可以在同一个查询中执行此操作,还是最好根据此结果进行另一个查询?
PARAMETERS [First Date] DateTime, [Second Date] DateTime, [Shift] Text ( 255 );
SELECT
Main.[Mth/day],
Main.Shift,
([Run Hrs]*[Actual Lbs/hr]) AS [Gross Production - LBS],
[Scrap Lbs 1]+[Scrap Lbs 2]+[Scrap Lbs 3] AS [Scrap Produced - LBS],
[Run Hrs]*[Actual Lbs/hr]-([Scrap Lbs 1]+[Scrap Lbs 2]+[Scrap Lbs 3]) AS [Net Production],
[Run Hrs]*[Actual Lbs/hr]*0.035 AS [Scrap Target],
[Run Hrs]*[Std lbs/hr]-([Run Hrs]*[Actual Lbs/hr])*0.035 AS [Target Net Production],
[Run Hrs]*[Std lbs/hr] AS [Target Gross Production],
(([Run Hrs]*[Std Lbs/hr])/([Run Hrs]*[Actual Lbs/hr]*0.035)) AS [Target Scrap Rate]
FROM Main
WHERE
(((Main.[Mth/day]) Between [First Date] And [Second Date]
And [Shift]=Main.Shift
And [Std lbs/hr]>0));
I need to also sum each calculated field so they can also retrieve monthly summaries and the like. Can i do this in the same query or am I best to make another query based off of this one's results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上应该在前端或报表编写器中处理,而不是通过 SQL。
在 MS Access 2007 中,应执行以下操作:
单元格应与聚合一起添加到报表页脚。
This is really something that should be handled in the front-end or report writer, not through SQL.
In MS Access 2007 the following should work:
A cell should be added to the report footer with your aggregate.
从仅使用不使用 SQL 聚合函数。然后保存该查询并将其用作另一个查询的数据源,您可以在其中进行求和和任何其他聚合函数。
在其他情况下,您可以仅从第一个查询中获取 SQL,并将其用作另一个查询中的子查询。然而,由于名称需要方括号,因此作为子查询会很麻烦。
将来,您可以通过选择不需要括号的对象名称来使自己更轻松。
Start with a query which uses only fields and field expressions which don't use SQL Aggregate Functions. Then save that query and use it as the data source for another query where you do your summing and any other aggregate functions.
In other cases, you could take just the SQL from the first query and use it as a subquery in another query. However, because of those square brackets required for the names, this one would be troublesome as a subquery.
In the future, you can make it easier on yourself by choosing object names which don't require bracketing.