SQL - 查找具有服务组合的帐户

SQL - Find Accounts with combination of Services

提前致歉,绝对是 SQL 的新手,我不确定我的措辞是否最好。
我有一个 table,其中列出了具有活动服务的帐户。例如,

账户 服务编号 服务名称
1111 00001 税务咨询
1111 00004 家庭保险
1112 00004 家庭保险
1111 00003 车险
1111 00002 账单支付
1112 00001 税务咨询

我正在尝试查找具有以下内容的帐户:
税务咨询、账单支付和家庭保险
或者
税务咨询、账单支付和汽车保险

所以他们需要有 '00001' 和 '00002' and/or '00003'/'00004'
感谢您的帮助!

您可以为此使用条件聚合。比较简洁的方法是:

select acct
from t
group by acct
having sum(case when serviceName = 'Tax Consulting' then 1 else 0 end) > 0 and
       sum(case when serviceName = 'Bill Pay' then 1 else 0 end) > 0 and
       sum(case when serviceName in ('Home Insurance', 'Auto Insurance') then 1 else 0 end) > 0;

如果您不想同时拥有保险和服务的账户不重复,那么您可以使用:

sum(case when serviceName in ('Home Insurance', 'Auto Insurance') then 1 else 0 end) = 1