可以在同一个查询中对计算字段求和吗?

发布于 2024-11-28 21:08:24 字数 872 浏览 0 评论 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));

我还需要对每个计算字段进行求和,以便他们还可以检索每月摘要等。我可以在同一个查询中执行此操作,还是最好根据此结果进行另一个查询?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-12-05 21:08:25

这实际上应该在前端或报表编写器中处理,而不是通过 SQL。

在 MS Access 2007 中,应执行以下操作:

  1. 在布局视图中打开报表
  2. 单击要总计的列中的单元格之一
  3. 格式选项卡上单击总计并选择求和

单元格应与聚合一起添加到报表页脚。

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:

  1. Open your report in layout view
  2. Click on one of the cells in the column that you want to total
  3. On the Format tab click on Totals and select Sum

A cell should be added to the report footer with your aggregate.

眼眸印温柔 2024-12-05 21:08:25

从仅使用不使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文