[英]Table Column value in where clause
I have two table我有两张桌子
Employee员工
EmployeeId![]() |
Name![]() |
Job![]() |
---|---|---|
123 ![]() |
David![]() |
yes![]() |
124 ![]() |
Timothy![]() |
yes![]() |
125 ![]() |
Maxi![]() |
No![]() |
126 ![]() |
Abie![]() |
EmployeeResidence员工宿舍
Id ![]() |
EmployeeId![]() |
EmployeeSpclField ![]() |
EmployeeSpclValue ![]() |
---|---|---|---|
1 ![]() |
123 ![]() |
Married![]() |
yes![]() |
2 ![]() |
123 ![]() |
Nationality![]() |
MEX![]() |
3 ![]() |
123 ![]() |
Job![]() |
Fitter![]() |
4 ![]() |
124 ![]() |
Married![]() |
yes![]() |
5 ![]() |
124 ![]() |
Nationality![]() |
ARG ![]() |
6 ![]() |
125 ![]() |
Job![]() |
Driver![]() |
I need a Sql query that returns 1 or 0 based on the Bind value Employee id and I need the following conditions to be validated我需要一个基于 Bind 值 Employee id 返回 1 或 0 的 Sql 查询,我需要验证以下条件
So how can i do that ?那我该怎么做呢? I am building something like this
我正在建造这样的东西
select 1 from Employee emp ,EmployeeResidence er
where
employeeid=:empid and emp.job is not null and
I am struggling to include EmployeeSpclField column value to check if it is not null and belongs to MEX我正在努力包含 EmployeeSpclField 列值来检查它是否不为 null 并且属于 MEX
I might avoid a join and instead use exists logic here:我可能会避免加入,而是在此处使用存在逻辑:
SELECT e.*
FROM Employee e
WHERE Job IS NOT NULL AND
EXISTS (SELECT 1 FROM EmployeeResidence er
WHERE er.EmployeeId = e.EmployeeId AND
er.EmployeeSpclField = 'Nationality' AND
er.EmployeeSpclValue = 'MEX');
Select a 0/1 flag选择一个 0/1 标志
select case when exists (
select 1
from Employee emp
join EmployeeResidence er
on emp.employeeid = er.employeeid and er.EmployeeSpclField = 'Nationality' and er.EmployeeSpclValue = 'MEX'
where emp.employeeid=:empid and emp.job is not null ) then 1
else 0 end flag
from dual;
Try this:尝试这个:
Select
e.EmployeeID,
case when er.EmployeeID is null then 0 else 1 end as return_val
from
Employee e
left outer join
(select EmployeeId,
max(case when EmployeeSpclField='Married' then EmployeeSpclValue else NULL end) as married,
max(case when EmployeeSpclField='Nationality' then EmployeeSpclValue else NULL end) as Nationality,
max(case when EmployeeSpclField='Job' then EmployeeSpclValue else NULL end) as Job
from
EmployeeResidence
group by
EmployeeId) er
on e.EmployeeId=er.EmployeeId
and e.Job is not null
and (er.Nationality is not null and er.Nationality ='MEX')
One option might be using conditional aggregation in order to count desired conditions一种选择可能是使用条件聚合来计算所需的条件
SELECT SIGN(NVL(SUM(CASE WHEN er.EmployeeSpclField = 'Nationality'
AND er.EmployeeSpclValue = 'MEX'
AND e.Job IS NOT NULL THEN 1 ELSE 0 END),0))
AS Result
FROM Employee e
JOIN EmployeeResidence er
ON er.EmployeeId = e.EmployeeId
WHERE e.EmployeeId = :empid
where SIGN()
function is used considering the possibility to have duplicate records for Nationality
value for EmployeeSpclField
column per any of EmployeeId
column's value.其中
SIGN()
函数是用来考虑可能有重复的记录Nationality
的价值EmployeeSpclField
每任何列EmployeeId
列的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.