购买了多种通行证的SQL Server 2016用户
我试图找出购买了多种通行证的用户。而不是他们购买了多少通行证 我的问题是:购买了多种通行证的SQL Server 2016用户,sql,sql-server-2016,Sql,Sql Server 2016,我试图找出购买了多种通行证的用户。而不是他们购买了多少通行证 我的问题是: select MemberNameFormatted, passtype from Memberships_View_With_Inactive As members inner join PassType AS PassType on PassType.serviceguid = members.ServiceGUID where PassType in ('Fee As
select
MemberNameFormatted,
passtype
from
Memberships_View_With_Inactive As members
inner join
PassType AS PassType on PassType.serviceguid = members.ServiceGUID
where
PassType in ('Fee Assistance Pass', 'All Access','Plus Pass','Value Pass')
group by
MemberNameFormatted, PassType
having
count(all(PassType)) >= 1
这将返回并显示所有用户及其持有的通行证类型:
我需要的是:只有持有两种或两种以上不同通行证类型的用户:
一个人可以每个月更新他们的会员资格,所以我对他们有多少会员资格不感兴趣,只要他们在任何时候持有不同的会员资格。大多数人坚持使用一种通行证,因此会员表中有数千个条目。如果您只需要会员,您可以使用group by,如您的问题所述:
select m.MemberNameFormatted, pt.passtype
from Memberships_View_With_Inactive m join
PassType pt
on pt.serviceguid = m.ServiceGUID
where pt.PassType in ('Fee Assistance Pass', 'All Access', 'Plus Pass', 'Value Pass')
group by m.MemberNameFormatted
having count(distinct pt.passtype) >= 2
如果你也想要通行证,那么:
with mp as (
select m.MemberNameFormatted, pt.passtype
from Memberships_View_With_Inactive m join
PassType pt
on pt.serviceguid = m.ServiceGUID
where pt.PassType in ('Fee Assistance Pass', 'All Access', 'Plus Pass', 'Value Pass')
)
select distinct mp.*
from mp
where exists (select 1
from mp mp2
where mp2.MemberNameFormatted= mp.MemberNameFormatted and mp2.passtype <> mp.passtype
);
您可以使用不同的
select distinct
t1.MemberNameFormatted,
t1.passtype
from
Table as t1
join
Table as t2 on
t2.MemberNameFormatted = t1.MemberNameFormatted and
t2.passtype <> t1.passtype
order by
t1.MemberNameFormatted,
t1.passtype