[英]SQL - Find Accounts with combination of Services
提前道歉,絕對是 SQL 的新手,我不確定我的措辭是最好的。
我有一張表格,其中列出了具有活動服務的帳戶。 例如,
帳戶 | 服務標識 | 服務名稱 |
---|---|---|
1111 | 00001 | 稅務咨詢 |
1111 | 00004 | 家庭保險 |
1112 | 00004 | 家庭保險 |
1111 | 00003 | 汽車保險 |
1111 | 00002 | 賬單支付 |
1112 | 00001 | 稅務咨詢 |
我正在嘗試查找具有以下內容的Accts:
稅務咨詢、賬單支付和家庭保險
或者
稅務咨詢、賬單支付和汽車保險
所以他們需要有'00001'和'00002'和/或'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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.