以下是我的桌子:
我想在select语句中创建一个具有以下条件的视图:
如果count(employee_id)> 1,则仅在视图中选择状态为'Current'的记录。
我试过:
select employee_id
, case when COUNT(employee_id) > 1 and statusval = 'Current' then 'Y' else 'N' end as val
from table1
group by employee_id
我希望有人可以帮助我发表这一声明。谢谢。
只是为了向你展示你的尝试看起来如何。你已经快到了,你只需要使用count
例如的窗口版本
with cte as (
select Employee_Id, [Status]
, case when count(*) over (partition by id) > 1 and [Status] = 'Current' then 1 else 0 end Val
from Table1
)
select Employee_Id, [Status]
from cte
where Val = 1;
从表面上看,这种方法比连接要好。