我想根据下面给出的某些条件从下表中获取数据。
EmployeeSalary Table
------------------
EmpId Salary
1 76500
2 73500
3 90000
4 79000
5 35000
GlobalData Table
------------------
Name Value
NewIncrmeent 20000
MaxIncrement 999999
EmployeeDetails Table
-----------------
EmpId Name Department Expereince
1 John IT 6
2 Kris BPO 5
3 Mark Facilities 3
4 Joe Security 10
获取员工详细信息(empid,salary,name,department,expereince)的列表,其工资+ NewIncrement(需要通过列名“NewIncrement”从GlobalData表中读取,其值现在为20000)大于给定员工的工资。
例如,如果查询的输入是empid值3,那么输出应该是3个记录,其中empid为1,2,4。
我可以通过使用sql过程获得所需的数据,但我想知道我们是否可以使用sql查询获取类似的数据。我尝试了加入概念但我无法获得正确的数据。
答案 0 :(得分:2)
像这样。
此外empid = 3
的薪水为90000。 90000 + 20000 = 110000所以所有行都将从EmployeeDetails
表
SELECT *
FROM EmployeeDetails ED
WHERE EXISTS (SELECT 1
FROM EmployeeSalary es
WHERE es.EmpId = ed.EmpId
AND es.salary < (SELECT salary
+ (SELECT value
FROM globaldate
WHERE Name = 'NewIncrmeent')
FROM EmployeeSalary es1
WHERE es1.empid = 3))
答案 1 :(得分:1)
SELECT e.*
FROM employeedetails e
JOIN employeesalary s
ON s.empid = e.empid
JOIN globaldata g
ON g.name = 'newincrmeent'
JOIN employeesalary x
ON x.empid <> s.empid
AND x.empid = 3
AND x.salary < s.salary+g.value;
答案 2 :(得分:1)
将@empID
作为变量
SELECT ED.EmpID
,ES.Salary
,ES.Salary + GD.NewIncrement AS ExpectedSalary
,ED.Name
,ED.Department
,ED.Experience
FROM EmployeeDetails ED
INNER JOIN EmployeeSalary ES
ON ED.EmpID = ES.EmpID
OUTER APPLY (
SELECT Value AS NewIncrement
FROM GlobalData
WHERE Name = 'NewIncrement') GD
WHERE ES.Salary + GD.NewIncrement > (SELECT Salary FROM EmployeeSalary WHERE EmpID = @empID)
AND ED.EmpID != @empID
答案 3 :(得分:0)
试试这个:
select ed.*,es.Salary from EmployeeDetails ed
inner join EmployeeSalary es on ed.EmpId=es.EmpId
where es.Salary < (
select sum(p.Salary ) from
(
select Salary from EmployeeSalary where EmpId =3
union all
select Value from GlobalData where Name ='NewIncrmeent'
) as p
);