1

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

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

4

2 回答 2

1

我无权访问 MsAccess,但我在 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
于 2012-10-09T22:07:44.410 回答
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;
于 2012-10-09T21:53:26.633 回答