Sql 如何将count group by与从两个表中获取结果的位置相结合
我正在尝试查找所有多次转介的租户的Sql 如何将count group by与从两个表中获取结果的位置相结合,sql,sql-server,Sql,Sql Server,我正在尝试查找所有多次转介的租户的全名,电子邮件id,电话号码和转介代码。有两个表,我正试图得到的数据已经提到了两次以上。我的代码没有错误符号,但在运行查询时,它会给我错误 除非是在一个集合中,否则集合不能出现在WHERE子句中 HAVING子句或select列表中包含的子查询,以及列 聚合是一个外部引用 在subselect中,应使用having作为过滤器聚合值 select dbo.Profiles.first_name + ' ' + dbo.Profiles.last_
全名
,电子邮件id
,电话号码
和转介代码
。有两个表,我正试图得到的数据已经提到了两次以上。我的代码没有错误符号,但在运行查询时,它会给我错误
除非是在一个集合中,否则集合不能出现在WHERE子句中
HAVING子句或select列表中包含的子查询,以及列
聚合是一个外部引用
在subselect中,应使用having作为过滤器聚合值
select
dbo.Profiles.first_name + ' ' + dbo.Profiles.last_name as full_name
,dbo.Profiles.email
, dbo.Profiles.phone
,dbo.Profiles.referral_code
from dbo.Profiles
where profile_id = (
select referrer_id
from dbo.Referrals
group by dbo.Referrals.referrer_id
HAVING COUNT(referrer_id)>2
)
在subselect中,应使用having作为过滤器聚合值
select
dbo.Profiles.first_name + ' ' + dbo.Profiles.last_name as full_name
,dbo.Profiles.email
, dbo.Profiles.phone
,dbo.Profiles.referral_code
from dbo.Profiles
where profile_id = (
select referrer_id
from dbo.Referrals
group by dbo.Referrals.referrer_id
HAVING COUNT(referrer_id)>2
)
- 我假设referer\u id是profile\u id的另一个名称
- 将下面的READ_替换为dbo.Referrals的唯一键字段
- 我假设referer\u id是profile\u id的另一个名称
- 将下面的READ_替换为dbo.Referrals的唯一键字段
select dbo.Profiles.first_name + ' ' + dbo.Profiles.last_name as
full_name,dbo.Profiles.email, dbo.Profiles.phone ,dbo.Profiles.referral_code
from dbo.Profiles
where profile_id =
(select referrer_id from dbo.Referrals
group by dbo.Referrals.referrer_id
having COUNT(READ_BELOW)>2
)
SELECT referrer_id, COUNT(*) AS 'Count_'
INTO #RefCount
FROM dbo.Referrals
GROUP BY referrer_id
SELECT dbo.Profiles.first_name + ' ' + dbo.Profiles.last_name AS 'Full_Name',
dbo.Profiles.email, dbo.Profiles.phone ,dbo.Profiles.referral_code
FROM dbo.Profiles, #RefCount RC
WHERE dbo.Profiles.profile_id = RC.referrer_id
AND RC.Count_ > 2