-2

我正在尝试查找所有角色 Role.Type = 2 且 Fees.Price 大于 Role.Type = 1 的员工在同一个地方(Role.PlaceID)。

表:员工

  • ID
  • 姓名
  • 类型

表:角色

  • 员工ID
  • 地点 ID
  • 类型

表:费用

  • 员工ID
  • 地点 ID
  • 价格

任何人都可以帮助我吗?

编辑:一些数据示例

员工

ID Name Type
1  Name1 1
2  Name2 1
3  Name3 1

角色

EmployeeID  PlaceID  Type
1            1        1
2            2        1
3            2        2

费用

EmployeeID  PlaceID Price
1             1      500
2             2      700
3             2      800

我的查询应该返回 (Name3) 作为结果,因为我们有多个员工在同一个地方,同时 Role.Type = 1 和 Role.Type = 2 并且 Name3 as Role.Type = 2 具有最大的 Fees.Price 然后 Role.Type = 1 .

4

3 回答 3

1

我不在我的办公桌前,所以我无法测试它,但它应该可以工作:

with EmplyeeWithMaxFee as
(
  select e.Id, e.Name, r.PlaceId, r.Type, Max(f.Price) as MaxFee
  from Emplyee e
  inner join Role r on r.EmployeeID = e.ID
  inner join Fees f on f.EmployeeID = e.ID
  group by e.Id, e.Name, r.PlaceId, r.Type
)

select e1.*
from EmplyeeWithMaxFee e1
where e1.Type = 2 and e1.MaxFee > (select Max(e2.MaxFee) from EmplyeeWithMaxFee e2 where e2.PlaceId = e1.PlaceId and e2.Id <> e1.Id and e2.Type = 1)
于 2013-06-11T14:00:14.537 回答
0

提供数据样本并指定“有更大的费用。价格”的含义。但是,也许,这样的事情会起作用:

    SELECT Employee.* FROM Employee INNER JOIN Role ON Employee.ID = Role.EmployeeID 
INNER JOIN Fees ON Role.EmployeeId = Fees.EmployeeID AND Role.PlaceID = Fees.PlaceID
    WHERE Role.Type = 2 AND Fees.Price >= All (Select Fees1.Price FROM Role Role1 
INNER JOIN Fees Fees1 ON Role1.PlaceID = Fees.PlaceID WHERE Role.Type = Role1.Type)
于 2013-06-11T14:01:18.700 回答
0

如果我正确理解了您的问题,那么像这样的查询将解决您的问题

SELECT EMP2.ID
FROM (SELECT ID, PLACEID, PRICE
      FROM EMPLOYEE, FEES
     WHERE EMPLOYEEID = ID AND TYPE = 2) EMP2,
   (SELECT ID, PLACEID, PRICE
      FROM EMPLOYEE, FEES
     WHERE EMPLOYEEID = ID AND TYPE = 1) EMP1
WHERE EMP2.PLACEID = EMP1.PLACEID AND EMP2.PRICE > EMP1.PRICE
于 2013-06-11T14:05:50.303 回答