在子查询中从分组依据中排除字段

Excluding fields from group by in a subquery

编辑

我在 Hockenberry 的帮助下完成了查询!谢谢。 但我还有一个问题。 我的时间计划table结构实际上如下。

Material no|Production time|Production place
12345|2,10|Robot
12345|7,40|Machining
67890|2,34|Machining
34567|9,93|Manuel

如果我的查询与 table 报告中的生产地点相匹配,我的查询只需要时间计划 table 中的时间。

我要查询结果将第二产地的生产时间加到按[生产日期]、[产地]、[班次]

分组的总和中

换句话说,我不想按 Report.ID、Report.[Creation date]、Report.[Responsible] 和 Report.[Number of Workers] 对查询进行分组,但是时间计划。[产地]。但我希望在从报告 table.

中获取的行中得到这个结果
 SELECT *, 

 (TotalInterruptDuration + TotalReworkDuration + TotalOvertimeDuration) AS TotalSum

 FROM (
SELECT 
    Report.ID, 
    Report.[Creation date], 
    Report.[Production date], 
    Report.[Production place],
    Report.[Shift], 
    Report.[Responsible],
    Report.[Number of workers],

    (SELECT Sum(Interrupts.[Interrupt duration]) 
     FROM Interrupts
     WHERE Interrupts.ID=Report.ID) AS TotalInterruptDuration,

    (SELECT Sum([Rework].[Rework duration]) 
     FROM [Rework]
     WHERE [Rework].ID=Report.ID) AS TotalReworkDuration, 

    (SELECT Sum(Overtime.[Overtime duration]) 
     FROM Rework
     WHERE Rework.ID=Report.ID) AS TotalOvertimeDuration,

    Year([Report]![Production date]) AS Year, 
    DatePart("ww",[Report]![Production date]-1) AS Week,   
    Month(Report![Production date]) AS Month,

    Sum(Quantity.[Quantity] * Timeplan.[Production.time]) AS CalcValue

FROM (Report 

     INNER JOIN Quantity ON Quantity.ID = Report.ID)
     INNER JOIN Timetable ON Report.[Production place] = Timetable.[Production place] AND Quantity.[Material no] = Timeplan.[Material no]
     GROUP BY Report.ID, Report.[Creation date], Report.[Production date],  Report.[Production place], Report.[Shift], Report.[Responsible], Report.     [Number of workers])
  tmp

................................................ .

首先抱歉我的英语不好。 我有一个查询如下。

SELECT 
    Report.ID, 
    Report.[Creation date], 
    Report.[Production date], 
    Report.[Production place],
    Report.[Shift], 
    Report.[Responsible],
    Report.[Number of workers],

    (SELECT Sum(Interrupts.[Interrupt duration]) 
     FROM Interrupts
     WHERE Interrupts.ID=Report.ID) AS TotalInterruptDuration,

    (SELECT Sum([Rework].[ Rework duration]) 
     FROM [Rework]
     WHERE [Rework].ID=Report.ID) AS TotalReworkDuration, 

    (SELECT Sum(Overtime.[Overtime duration]) 
     FROM Overtime
     WHERE Overtime.ID=Report.ID)  AS TotalOvertimeDuration,

    Year([Report]![Production date]) AS Year, 
    DatePart("ww",[Report]![Production date]-1) AS Week,  
    Month(Report![Production date]) AS Month

FROM Report

我的第一个问题是 我想在同一行中对 TotalInterruptDuration、TotalReworkDuration、TotalOvertimeDuration 求和。

第二个问题更重要; 如果 ID 匹配,我想加入报告和数量 table,并通过加入 [数量] 和 [时间计划] 将报告。[工人人数] 与从相应 [Material 否] 得出的 [生产时间] 的总和相乘] tables.

例如,我想查看 id(1) 的 table 数量,然后将 material 12345 的生产时间乘以 10(数量)和 67890 的生产时间,然后将其乘以 11(数量)和 34567 的生产时间并将其乘以 7(数量)和总和值并在查询中打印。

我的 table 看起来像这样。

    **Table Report**

    *ID     Creation date   Production date Production place    Shift   Responsible Number of workers*


    1   01.01.2015  01.01.2015      Robot           2   Omer        12

    2   02.01.2015  02.01.2015      Robot           3   Erdem       15

    3   03.01.2015  03.01.2015      Machining       2   Sukru       4

    4

    **Table Quantity**


    *ID Quantity    Material No*

    1   10      12345

    1   11      67890

    1   7       34567

    2   3       12345

    3   6       67890

    3   6       34567

4   5       12345

    **Table Timeplan**

        *Material No    Production Time*

        12345        34

        67890        11

        34567        21

编辑问题 1:

SELECT *, 
    TotalSum = (TotalInterruptDuration + TotalReworkDuration + TotalOvertimeDuration)
FROM (
    SELECT 
        Report.ID, 
        Report.[Creation date], 
        Report.[Production date], 
        Report.[Production place],
        Report.[Shift], 
        Report.[Responsible],
        Report.[Number of workers],

        (SELECT Sum(Interrupts.[Interrupt duration]) 
         FROM Interrupts
         WHERE Interrupts.ID=Report.ID) AS TotalInterruptDuration,

        (SELECT Sum([Rework].[ Rework duration]) 
         FROM [Rework]
         WHERE [Rework].ID=Report.ID) AS TotalReworkDuration, 

        (SELECT Sum(Overtime.[Overtime duration]) 
         FROM Overtime
         WHERE Overtime.ID=Report.ID)  AS TotalOvertimeDuration,

        Year([Report]![Production date]) AS Year, 
        DatePart("ww",[Report]![Production date]-1) AS Week,  
        Month(Report![Production date]) AS Month

    FROM Report
    ) tmp

问题 2:

SELECT
     Report.*       -- all rows from table Report
--  ,Quantity.*     -- all rows from table Quantity
--  ,Timeplan*      -- all rows from table Timeplan

    ,CalcValue = Report.[Number of Workers] * Timeplan.[Production Time]

FROM Report
INNER JOIN Quantity ON Quantity.ID = Report.ID
INNER JOIN Timeplan ON TimePlan.[Material No] = Quantity.[Material No]

如果您需要查看 QuantityTimeplan table 中的列,请删除 --。 Reporttable 仅显示与 Quantity-table 和 Timeplan-table.

匹配的行

编辑 两个查询合并。

SELECT *, 
    TotalSum = (TotalInterruptDuration + TotalReworkDuration + TotalOvertimeDuration)
FROM (
    SELECT 
        Report.ID, 
        Report.[Creation date], 
        Report.[Production date], 
        Report.[Production place],
        Report.[Shift], 
        Report.[Responsible],
        Report.[Number of workers],

        (SELECT Sum(Interrupts.[Interrupt duration]) 
         FROM Interrupts
         WHERE Interrupts.ID=Report.ID) AS TotalInterruptDuration,

        (SELECT Sum([Rework].[ Rework duration]) 
         FROM [Rework]
         WHERE [Rework].ID=Report.ID) AS TotalReworkDuration, 

        (SELECT Sum(Overtime.[Overtime duration]) 
         FROM Overtime
         WHERE Overtime.ID=Report.ID)  AS TotalOvertimeDuration,

        Year([Report]![Production date]) AS Year, 
        DatePart("ww",[Report]![Production date]-1) AS Week,  
        Month(Report![Production date]) AS Month,

        Report.[Number of Workers] * Timeplan.[Production Time] AS CalcValue
    FROM
         (Report
         INNER JOIN Quantity 
         ON Quantity.ID = Report.ID)
         INNER JOIN Timeplan 
         ON TimePlan.[Material No] = Quantity.[Material No]
    ) tmp

EDIT 更新了 join 以供访问。 请注意准入条件: Multiple INNER JOIN SQL ACCESS