使用sum()表连接两个表

时间:2012-10-09 21:41:04

标签: sql database

我目前有一个查询,显示我团队中的人员及其当前角色(从另一个表中查找):

SELECT firstName + ' ' + lastName as Name,
       RTS_ROLE.roleDescription as `Current Role`, 
FROM RTS_STAFF, RTS_ROLE
WHERE RTS_STAFF.staffRole = RTS_ROLE.roleID 
AND staffDeleted <> true
ORDER BY roleID;

另一个查询列出了每个人每周工作时间:

SELECT RTS_STAFF.staffId, Sum(IIf(Isnull(Monday),0,Monday)
+IIf(Isnull(Tuesday),0,Tuesday)
+IIf(Isnull(Wednesday),0,Wednesday)
+IIf(Isnull(Thursday),0,Thursday)
+IIf(Isnull(Friday),0,Friday)) AS `Weekly Hours`
FROM WORKING_HOURS, RTS_STAFF
WHERE RTS_STAFF.staffId = WORKING_HOURS.staffID 
AND Date() > WORKING_HOURS.EffectiveFrom 
GROUP BY RTS_STAFF.staffId;

我已尝试(不成功)加入这些查询,方法是将第二个查询作为子查询放在第一个查询中但没有用:我得到了'你已经编写了一个子查询,可以返回多个字段而不使用EXIST主查询中的关键字......“

我正在尝试获取一个查询,该查询将显示同一查询中的员工姓名,角色名称和“当前”总工作时间。有人能告诉我这是如何实现的,或者我做错了什么(毫无疑问是一件令人尴尬的事情)。

很高兴提供更多信息,如果有帮助的话。

Table structure:
RTS_ROLE: (lookup table)
(pk)RoleID: number
roleDescription: string

RTS_STAFF
(pk)staffId: number
firstName: string
lastName: string
staffRole: number (fk to RTS_ROLE.roleID)
staffDeleted: boolean

WORKING_HOURS:
(pk)workingWeekID: number
staffID: number (fk to RTS_STAFF.staffId)
Monday: number
Tuesday: number
...
Friday:number
EffectiveFrom: date

我们正在使用(不要笑)MS Access 2007

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

SELECT firstName + ' ' + lastName as Name,
   RTS_ROLE.roleDescription as `Current Role`, x.weeklyhours
   FROM RTS_STAFF s INNER JOIN
   RTS_ROLE r ON s.staffRole = r.roleID INNER JOIN
   (
     SELECT RTS_STAFF.staffId, Sum(IIf(Isnull(Monday),0,Monday)
     +IIf(Isnull(Tuesday),0,Tuesday)
     +IIf(Isnull(Wednesday),0,Wednesday)
     +IIf(Isnull(Thursday),0,Thursday)
     +IIf(Isnull(Friday),0,Friday)) AS weeklyhours
     FROM WORKING_HOURS, RTS_STAFF
     WHERE RTS_STAFF.staffId = WORKING_HOURS.staffID 
     AND Date() > WORKING_HOURS.EffectiveFrom 
     GROUP BY RTS_STAFF.staffId
   ) x ON x.staffID = s.staffID
WHERE  s.staffDeleted <> true
ORDER BY r.roleID;

答案 1 :(得分:1)

我无法访问Ms Access但我在MySQL中准备了一些东西(你的IIF类似于MySQL IF):

SELECT staff.staffid,
       staff.firstname + ' ' + staff.lastname AS Name,
       role.roledescription                   AS `Current Role`,
       Sum(IF(Isnull(monday), 0, monday)
           + IF(Isnull(tuesday), 0, tuesday)
           + IF(Isnull(wednesday), 0, wednesday)
           + IF(Isnull(thursday), 0, thursday)
           + IF(Isnull(friday), 0, friday))   AS `Weekly Hours`
FROM   working_hours,
       rts_staff staff,
       rts_role role
WHERE  staff.staffrole = role.roleid
       AND staff.staffdeleted <> true
       AND staff.staffid = working_hours.staffid
       AND Now() > working_hours.effectivefrom
GROUP  BY staff.staffid,
          staff.firstname + ' ' + staff.lastname,
          role.roledescription 

基于这种结构:

CREATE TABLE `rts_staff` (
  `staffid` int(11) NOT NULL,
  `staffrole` int(11) DEFAULT NULL,
  `firstname` varchar(45) DEFAULT NULL,
  `lastname` varchar(45) DEFAULT NULL,
  `staffdeleted` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`staffid`)
) ENGINE=InnoDB


CREATE TABLE `rts_role` (
  `roleid` int(11) NOT NULL,
  `roledescription` varchar(45) DEFAULT NULL,
  PRIMARY KEY (`roleid`)
) ENGINE=InnoDB


CREATE TABLE `working_hours` (
  `staffid` int(11) NOT NULL,
  `effectivefrom` date DEFAULT NULL,
  `monday` int(11) DEFAULT NULL,
  `tuesday` int(11) DEFAULT NULL,
  `wednesday` int(11) DEFAULT NULL,
  `thursday` int(11) DEFAULT NULL,
  `friday` int(11) DEFAULT NULL,
  PRIMARY KEY (`staffid`)
) ENGINE=InnoDB