在子查询中排除group by中的字段

时间:2015-02-19 20:27:41

标签: sql ms-access join subquery

编辑

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

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

我的查询只是从timeplan表中获取时间,如果它与表格报告中的生产地点匹配。

我希望查询结果通过[生产日期],[生产地点],[转换]

将第二个生产地点的生产时间添加到总和分组中

换句话说,我不想按Report.ID,报告。[创建日期],报告。[负责]和报告。[工人数量],时间计划对查询进行分组。[生产地]。但是我希望这个结果出现在从报告表中获取的那一行。

 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

.............................................. < / p>

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

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

我的第一个问题是 我想将Total TotalInterruptDuration,TotalReworkDuration,TotalOvertimeDuration放在同一行。

第二个问题更为重要; 如果ID匹配,我想加入报告和数量表,并通过加入[数量]和[时间表]表,将[工人数量]与[生产时间]的总和从[相应的[材料编号])相加。

例如,我想查看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 个答案:

答案 0 :(得分:1)

编辑问题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]

如果您需要查看--Quantity表中的列,请删除Timeplan。 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

编辑更新了join以获取访问权限。 请注意访问加入条件: Multiple INNER JOIN SQL ACCESS