SQL - 从两个不同的表中求和

时间:2014-10-31 01:49:52

标签: mysql sql-server database

我试图在两个不同的表中总结两个不同属性中的值,并且遇到问题。所以我的表结构是:

位置

  • positionID(PK)

StaffRequest

  • requestID(PK)
  • positionID(来自职位的FK)

招聘

  • recruitmentID(PK)
  • requestID(来自StaffRequest的FK)

RegularRecruitment

  • 募集ID(PK,FK来自招聘)
  • numberOfWeeks

RegRecruitWeeklyTime

  • ID(PK)
  • recruitmentID(来自RegularRecruitment的FK)
  • 小时

因此,这5个表与我正在尝试执行的查询相关。 RegRecruitWeeklyTime表将包含正式员工的每周时间,并总结特定recruitmentID的小时数将为我们提供每周招聘工作的总小时数。而且numberOfWeeks表会给你新兵假设工作的小时数。

现在我想写一个查询,它会给我总小时数和总周数,并根据他们的位置类型对它们进行分组。这必须在单个查询中完成。

所以,起初我写了两个查询来执行每次求和。

SELECT
  Position.type AS 'Position',
  SUM(RegularRecuitment.numberOfWeeks) AS 'Total Weeks'
FROM Position, StaffRequest, Recruitment, RegularRecuitment
WHERE 
  Recruitment.recruitmentID = RegularRecuitment.recruitmentID 
  AND
  StaffRequest.requestID = Recruitment.requestID
  AND
  Position.positionID = StaffRequest.positionID
GROUP BY Position.type;

SELECT 
  Position.type AS 'Position',
  SUM(RegRecruitWeeklyTime.hours) AS 'Total hours'
FROM Position, StaffRequest, Recruitment, RegularRecuitment, RegRecruitWeeklyTime
WHERE
  RegRecruitWeeklyTime.recruitmentID = RegularRecuitment.recruitmentID
  AND
  Recruitment.recruitmentID = RegularRecuitment.recruitmentID 
  AND
  StaffRequest.requestID = Recruitment.requestID
  AND
  Position.positionID = StaffRequest.positionID
GROUP BY Position.type;

如果我单独运行这两个查询,它工作正常并给我正确的结果。但是,当我将这两个查询组合在一起时:

SELECT 
  Position.type AS 'Position', 
  SUM(RegRecruitWeeklyTime.hours) AS 'Total hours each week',
  SUM(RegularRecuitment.numberOfWeeks) AS 'Total Weeks'
FROM Position, StaffRequest, Recruitment, RegRecruitWeeklyTime, RegularRecuitment
WHERE
  RegRecruitWeeklyTime.recruitmentID = RegularRecuitment.recruitmentID
  AND
  Recruitment.recruitmentID = RegularRecuitment.recruitmentID 
  AND
  StaffRequest.requestID = Recruitment.requestID
  AND
  Position.positionID = StaffRequest.positionID
GROUP BY Position.type;

'Total Weeks'列乘以RegRecruitWeeklyTime中的行数,表示结果为SUM(RegularRecuitment.numberOfWeeks) * COUNT(RegRecruitWeeklyTime.hours)。如果我将“总周数”列值除以COUNT(RegRecruitWeeklyTime.hours),我会得到正确答案,但我只是想知道是否有更好的方法可以做到这一点。

...谢谢

2 个答案:

答案 0 :(得分:0)

你正在使用什么数据库?

如果您使用的是sqlserver

你总是可以这样做

SELECT 
(
  SELECT
    Position.type AS 'Position',
    SUM(RegularRecuitment.numberOfWeeks) AS 'Total Weeks'
  FROM Position, StaffRequest, Recruitment, RegularRecuitment
  WHERE
    Recruitment.recruitmentID = RegularRecuitment.recruitmentID 
    AND
    StaffRequest.requestID = Recruitment.requestID
    AND
    Position.positionID = StaffRequest.positionID
  GROUP BY Position.type
) AS select1,

(
  SELECT
    Position.type AS 'Position',
    SUM(RegRecruitWeeklyTime.hours) AS 'Total hours'
  FROM Position, StaffRequest, Recruitment, RegularRecuitment, RegRecruitWeeklyTime
  WHERE
    RegRecruitWeeklyTime.recruitmentID=RegularRecuitment.recruitmentID
    AND
    Recruitment.recruitmentID = RegularRecuitment.recruitmentID 
    AND
    StaffRequest.requestID = Recruitment.requestID
    AND
    Position.positionID = StaffRequest.positionID
  GROUP BY Position.type
) AS select2

答案 1 :(得分:0)

问题是找到'Total hours' RegRecruitWeeklyTime表与RegularRecuitment结合以获得结果,但查找'Total Weeks'您没有加入RegRecruitWeeklyTime表。当你尝试组合查询时,'total weeks'会有所不同。试试这个..

SELECT A.Position,
       A.Total_Weeks,
       A.Total_hours
FROM   (SELECT Position.type                        AS 'Position',
               Sum(RegularRecuitment.numberOfWeeks) AS 'Total_Weeks'
        FROM   Position
               JOIN StaffRequest
                 ON Position.positionID = StaffRequest.positionID
               JOIN Recruitment
                 ON StaffRequest.requestID = Recruitment.requestID
               JOIN RegularRecuitment
                 ON Recruitment.recruitmentID = RegularRecuitment.recruitmentID
        GROUP  BY Position.type) a
       JOIN (SELECT Position.type                   AS 'Position',
                    Sum(RegRecruitWeeklyTime.hours) AS 'Total_hours'
             FROM   Position
                    JOIN StaffRequest
                      ON Position.positionID = StaffRequest.positionID
                    JOIN Recruitment
                      ON StaffRequest.requestID = Recruitment.requestID
                    JOIN RegularRecuitment
                      ON Recruitment.recruitmentID = RegularRecuitment.recruitmentID
                    JOIN RegRecruitWeeklyTime
                      ON RegRecruitWeeklyTime.recruitmentID = RegularRecuitment.recruitmentID
             GROUP  BY Position.type) b
         ON A.Position = B.Position