需要查询中的所有行,但特定列仅需要第一行

时间:2018-10-25 12:36:54

标签: sql-server

我正在一个表上运行查询,该表返回要在其上使用SUM的多行,因此我只返回了1行。我可以做到这一点,并使用以下代码获得正确的结果:

SELECT E10Production.Erp.LaborDtl.EmployeeNum,
  E10Production.Erp.LaborDtl.JobNum,
  E10Production.Erp.LaborDtl.OprSeq,
  SUM(E10Production.Erp.LaborDtl.LaborHrs * E10Production.Erp.LaborDtl.LaborRate) AS TotalLabor,
  SUM(E10Production.Erp.LaborDtl.BurdenHrs * E10Production.Erp.LaborDtl.BurdenRate) AS BurdenCost
FROM E10Production.Erp.LaborDtl
  LEFT OUTER JOIN E10Production.Erp.JobOper ON E10Production.Erp.JobOper.Company = E10Production.Erp.LaborDtl.Company
    AND E10Production.Erp.JobOper.OprSeq = E10Production.Erp.LaborDtl.OprSeq 
    AND E10Production.Erp.JobOper.JobNum = E10Production.Erp.LaborDtl.JobNum
  LEFT OUTER JOIN E10Production.Erp.JobMtl ON E10Production.Erp.JobMtl.RelatedOperation = E10Production.Erp.LaborDtl.OprSeq 
    AND E10Production.Erp.LaborDtl.Company = E10Production.Erp.JobMtl.Company
    AND E10Production.Erp.LaborDtl.JobNum = E10Production.Erp.JobMtl.JobNum
WHERE E10Production.Erp.JobOper.JobNum = '623165' 
  AND E10Production.Erp.JobOper.OprSeq = '20'
  AND E10Production.Erp.JobMtl.MtlSeq = '20'
GROUP BY E10Production.Erp.LaborDtl.EmployeeNum,
  E10Production.Erp.LaborDtl.JobNum,
  E10Production.Erp.LaborDtl.OprSeq

原始数据如下:

EmployeeNum    JobNum    OprSeq    TotalLabor    BurdenCost
007349         623165    20        41.34         29.64
007349         623165    20        76.85         55.10

将所有内容分组并加总后,它看起来像这样:

EmployeeNum    JobNum    OprSeq    TotalLabor    BurdenCost
007349         623165    20        118.19        84.74

EmployeeNum,JobNum和OprSeq相同,TotalLabor和BurdenCost求和,降到1行,这正是我想要的。

现在我要在结果中再添加一列

  

E10Production.Erp.JobMtl.TotalCost AS MaterialCost。

所以我将列添加到查询的SELECT和GROUP BY中,现在看起来像这样:

SELECT E10Production.Erp.LaborDtl.EmployeeNum,
  E10Production.Erp.LaborDtl.JobNum,
  E10Production.Erp.LaborDtl.OprSeq,
  Sum(E10Production.Erp.LaborDtl.LaborHrs * E10Production.Erp.LaborDtl.LaborRate) AS TotalLabor,
  Sum(E10Production.Erp.LaborDtl.BurdenHrs * E10Production.Erp.LaborDtl.BurdenRate) AS BurdenCost,
  E10Production.Erp.JobMtl.TotalCost AS MaterialCost
FROM E10Production.Erp.LaborDtl
  LEFT OUTER JOIN E10Production.Erp.JobOper ON E10Production.Erp.JobOper.Company = E10Production.Erp.LaborDtl.Company
    AND E10Production.Erp.JobOper.OprSeq = E10Production.Erp.LaborDtl.OprSeq
    AND E10Production.Erp.JobOper.JobNum = E10Production.Erp.LaborDtl.JobNum
  LEFT OUTER JOIN E10Production.Erp.JobMtl ON E10Production.Erp.JobMtl.RelatedOperation = E10Production.Erp.LaborDtl.OprSeq
    AND E10Production.Erp.LaborDtl.Company = E10Production.Erp.JobMtl.Company
    AND E10Production.Erp.LaborDtl.JobNum = E10Production.Erp.JobMtl.JobNum
WHERE E10Production.Erp.JobOper.JobNum = '623165' 
  AND E10Production.Erp.JobOper.OprSeq = '20'
  AND E10Production.Erp.JobMtl.MtlSeq = '20'
GROUP BY E10Production.Erp.LaborDtl.EmployeeNum,
  E10Production.Erp.LaborDtl.JobNum,
  E10Production.Erp.LaborDtl.OprSeq,
  E10Production.Erp.JobMtl.TotalCost,
  E10Production.Erp.LaborDtl.LaborHrs * E10Production.Erp.LaborDtl.LaborRate,
  E10Production.Erp.LaborDtl.BurdenHrs * E10Production.Erp.LaborDtl.BurdenRate,
  MaterialCost

就像EmployeeNum,JobNum和OprSeq一样,TotalCost也一样,所以我希望看到这样的东西:

EmployeeNum    JobNum    OprSeq    TotalLabor    BurdenCost    MaterialCost
007349         623165    20        118.19        84.74         101.26

但是,我得到的却是这个结果:

EmployeeNum    JobNum    OprSeq    TotalLabor    BurdenCost    MaterialCost
007349         623165    20        41.34         29.64         101.26
007349         623165    20        76.85         55.10         101.26

对于我的一生,我不知道为什么会这样。我不知道它在某个地方是否是不正确的JOIN(我会承认,JOIN仍然让我陷入循环),或者这也许仅仅是SQL的局限性而不能满足我的要求。

有人能指出我正确的方向吗?我可以查询一些关键字吗?

0 个答案:

没有答案
相关问题