我正在尝试查找角色为Role.Type = 2并且具有更大费用的所有员工.Price然后在同一位置(Role.PlaceID)上具有Role.Type = 1的员工。
表:员工
表:角色
表:费用
任何人都可以帮助我吗?
编辑:一些数据示例
员工
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也是Role.Type = 2具有最大的费用。价格然后是角色。 Type = 1。
答案 0 :(得分: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)
答案 1 :(得分: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)
答案 2 :(得分: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