Helllo 以下查询在加入多个表后返回所有员工
select e.*
from dbo.EMP e
join dbo.HREMP a
on a.ID = e.ID
join dbo.LOGO c
on c.EMPID = e.id
join dbo.LOGOACC d
on d.BADGENO = c.BADGENO
and d.ACCLVID in (2191, 2292, 2293, 2294, 2295, 2296, 2297)
共有653条记录正在返回
现在执行以下查询后
with EmployeeCTE as
(
select e.*
from dbo.EMP e
join dbo.HREMP a
on a.ID = e.ID
join dbo.LOGO c
on c.EMPID = e.id
join dbo.LOGOACC d
on d.BADGENO = c.BADGENO
and d.ACCLVID in (2191, 2292, 2293, 2294, 2295, 2296, 2297)
)
select k.id from EmployeeCTE k
group by ID
having count (k.id) >1
我发现有135条记录的数量超过1条。
如何列出所有具有多个计数的记录?
答案 0 :(得分:2)
select *
from (select e.*
, count(*) over (partition by e.id) as cnt
from dbo.EMP e
join dbo.HREMP a
on a.ID = e.ID
join dbo.LOGO c
on c.EMPID = e.id
join dbo.LOGOACC d
on d.BADGENO = c.BADGENO
and d.ACCLVID in (2191, 2292, 2293, 2294, 2295, 2296, 2297)
) tt
where tt.cnt > 1