1

我有这个查询来表达一组业务规则。为了获得我需要的信息,我尝试加入表本身,但这会带回比表中实际更多的记录。以下是我尝试过的查询。我究竟做错了什么?

SELECT DISTINCT a.rep_id, a.rep_name, count(*) AS 'Single Practitioner'
FROM [SE_Violation_Detection] a inner join [SE_Violation_Detection] b 
ON a.rep_id = b.rep_id and a.hcp_cid = b.hcp_cid
group by a.rep_id, a.rep_name
having count(*) >= 2
4

2 回答 2

4

您可以使用 having 子句来完成此操作:

select a, b, count(*) c
from etc
group by a, b
having count(*) >= some number
于 2013-03-20T17:25:01.167 回答
1

我想出了一种更简单的方法来获取其中一个查询所需的信息。上面那个还是错的。

--Rep violation for different HCP more than 5 times 
select distinct rep_id,rep_name,count(distinct hcp_cid) 
AS 'Multiple Practitioners' 
from dbo.SE_Violation_Detection
group by rep_id,rep_name 
having count(distinct hcp_cid)>4 
order by count(distinct hcp_cid)
于 2013-03-21T19:52:10.420 回答