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

Sql 在子查询中排除分组依据中的字段,sql,ms-access,join,subquery,Sql,Ms Access,Join,Subquery,编辑 我在Hockenberry的帮助下完成了查询!谢谢 但我还有一个问题。 我的时间表结构实际上如下 Material no|Production time|Production place 12345|2,10|Robot 12345|7,40|Machining 67890|2,34|Machining 34567|9,93|Manuel SELECT Report.ID, Report.[Creation date], Report.[Production

编辑

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

Material no|Production time|Production place
12345|2,10|Robot
12345|7,40|Machining
67890|2,34|Machining
34567|9,93|Manuel
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
如果我的查询与表报告中的生产地点相匹配,则只从时间计划表中获取时间

我希望查询结果按[生产日期]、[生产地点]、[班次]将第二个生产地点的生产时间添加到总和分组中

换句话说,我不想按Report.ID、Report.[Creation date]、Report.[Responsible]和Report.[Number of worker]、时间计划。[生产地点]对查询进行分组。但我希望在从报表表中获取的行上显示此结果

 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

首先为我的英语不好感到抱歉。 我有一个问题如下

Material no|Production time|Production place
12345|2,10|Robot
12345|7,40|Machining
67890|2,34|Machining
34567|9,93|Manuel
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匹配,我想联接报表和数量表,并通过联接[Quantity]和[Timeplan]表,将报表。[工人数量]与从相应的[Material no]派生的[Production time]之和相乘

例如,我想在表格数量中查找id(1),然后将物料12345的生产时间乘以10(数量),生产时间乘以67890,再乘以11(数量),生产时间乘以34567,再乘以7(数量)和总值,然后在查询中打印

我的桌子看起来像这样

    **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]
如果需要查看
数量
时间计划
表中的列,请删除
--
。 Reporttable只显示与Quantity表和Timeplan表匹配的行

编辑合并两个查询

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
编辑为访问更新了
加入
。 请注意访问加入条件:

我想将您的查询附加到我的查询中。我该怎么做?我的意思是将query1和Query2结合在一起请注意访问连接条件。你可以在这里找到一个例子(但我希望我的上一次更新可以帮助你)。如果您的问题已成功回答,则应关闭该问题并创建一个新问题。如果你开始一个新的问题,它位于当前开放问题的顶部,你会吸引更多人的注意。如果你编辑你的(已经回答的)问题,它不会引起太多的注意。但我有空的时候会去问你的新问题(我想这会在一到两个小时内)。