Sql 查找仅存在2个值的记录
我的表结构/数据集是这样的Sql 查找仅存在2个值的记录,sql,oracle,relational-division,Sql,Oracle,Relational Division,我的表结构/数据集是这样的 Emp_id Expense_amt_dollar Dept 1111 100 Marketing 1111 75 Finance 1111 25 IT 2222 100 Marketing 3333 50 Finance 4444 30
Emp_id Expense_amt_dollar Dept
1111 100 Marketing
1111 75 Finance
1111 25 IT
2222 100 Marketing
3333 50 Finance
4444 30 Marketing
4444 70 Finance
5555 200 IT
O/p
我只在2个部门寻找Emp费用
Emp_id Expense_amt_dollar Dept
1111 100 Marketing
1111 75 Finance
4444 30 Marketing
4444 70 Finance
只有这两个部门有记录的Emp。
两个部门的记录均应具有
存在的:
select t.* from tablename t
where t.dept in ('Finance', 'Marketing')
and exists (
select 1 from tablename
where emp_id = t.emp_id and dept in ('Finance', 'Marketing') and dept <> t.dept
)
存在时
:
select t.* from tablename t
where t.dept in ('Finance', 'Marketing')
and exists (
select 1 from tablename
where emp_id = t.emp_id and dept in ('Finance', 'Marketing') and dept <> t.dept
)
您可以选择“市场营销”和“财务”行,然后检查是否为员工同时获得这两个行:
select *
from
(
select emp_id, dept, count(*) over (partition by emp_id) as cnt
from mytable t
where dept in ('Marketing', 'Finance')
)
where cnt = 2
order by emp_id, dept;
您可以选择“市场营销”和“财务”行,然后检查是否为员工同时获得这两个行:
select *
from
(
select emp_id, dept, count(*) over (partition by emp_id) as cnt
from mytable t
where dept in ('Marketing', 'Finance')
)
where cnt = 2
order by emp_id, dept;
只有这两个部门有记录的Emp。两个部门都应有记录
我将使用窗口功能:
select emp_id, expense_amt_dollar, dept
from (
select
e.*,
sum(case when dept in ('Marketing', 'Finance') then 1 end) over(partition by emp_id) cnt_deps
sum(case when dept not in ('Marketing', 'Finance') then 1 end) over(partition by emp_id) cnt_other_deps
from emp e
) e
where cnt_deps = 2 an cnt_other_deps is null
这将为您提供属于两个部门而不属于其他部门的员工的记录-这就是我对您的问题的理解。为此,您需要查看整个表:使用where
子句过滤将阻止您检查该员工是否不属于任何其他部门
只有这两个部门有记录的Emp。两个部门都应有记录
我将使用窗口功能:
select emp_id, expense_amt_dollar, dept
from (
select
e.*,
sum(case when dept in ('Marketing', 'Finance') then 1 end) over(partition by emp_id) cnt_deps
sum(case when dept not in ('Marketing', 'Finance') then 1 end) over(partition by emp_id) cnt_other_deps
from emp e
) e
where cnt_deps = 2 an cnt_other_deps is null
这将为您提供属于两个部门而不属于其他部门的员工的记录-这就是我对您的问题的理解。为此,您需要查看整个表:使用where
子句进行过滤将阻止您检查员工是否不属于任何其他部门。另一个选项是使用having count(distinct dept)=2
子句生成子查询,然后与同一个表联接
select t1.*
from tab t1
join (select emp_id, max(dept) dept1, min(dept) dept2
from tab
where dept in ('Finance', 'Marketing')
group by emp_id
having count(distinct dept) = 2) t2
on t1.emp_id = t2.emp_id
and t1.dept in (t2.dept1, t2.dept2)
另一个选项是生成一个子查询,其中具有count(distinct dept)=2
子句,然后与同一个表联接
select t1.*
from tab t1
join (select emp_id, max(dept) dept1, min(dept) dept2
from tab
where dept in ('Finance', 'Marketing')
group by emp_id
having count(distinct dept) = 2) t2
on t1.emp_id = t2.emp_id
and t1.dept in (t2.dept1, t2.dept2)
曾在Postgresql工作:
select *
from employee_expenses
where emp_id in
(
select emp_id from employee_expenses
where dept = 'Finance'
intersect
select emp_id from employee_expenses
where dept ='Marketing'
)
and dept in ('Finance', 'Marketing');
曾在Postgresql工作:
select *
from employee_expenses
where emp_id in
(
select emp_id from employee_expenses
where dept = 'Finance'
intersect
select emp_id from employee_expenses
where dept ='Marketing'
)
and dept in ('Finance', 'Marketing');
我假设Shyam只指两个部门的员工,而不是两个部门的员工。因此,预期结果中的员工1111。我假设Shyam仅指两个部门的员工,而不是仅指两个部门的员工。这是一个常见问题解答。在考虑发帖之前,请阅读本手册,谷歌搜索任何错误信息,或您的问题/问题/目标的许多清晰、简洁和准确的措辞,包括或不包括您的特定字符串/名称和网站:stackoverflow.com&tags;阅读许多答案。如果你发布一个问题,用一句话作为标题。反思你的研究。请参阅文本上方的投票箭头鼠标(&T)。这是常见问题解答。在考虑发帖之前,请阅读本手册,谷歌搜索任何错误信息,或您的问题/问题/目标的许多清晰、简洁和准确的措辞,包括或不包括您的特定字符串/名称和网站:stackoverflow.com&tags;阅读许多答案。如果你发布一个问题,用一句话作为标题。反思你的研究。请参见文本上方的投票箭头(&S)。