我的这个表包含用户电子邮件和公司ID。用户可以在其帐户中添加多个公司。因此,用户可以在其电子邮件中拥有多个公司ID。 我想写一个查询来计算用户(我已经编写过这个查询),并将它们标识为多个公司用户。 因此,在我的COUNT语句中,我应该如何写,如果该电子邮件中有多个公司ID标识为多公司用户。
以下是我为了获取具有多个Id的电子邮件列表而编写的查询。
SELECT email, COUNT(company_id) AS count_of_companies
FROM database
GROUP BY email
HAVING COUNT(company_id) > 1
ORDER BY count_of_companies DEC;
谢谢
答案 0 :(得分:0)
假设您需要识别并将用户列表作为多公司获取,我已修改您的查询
SELECT EmailID, CASE WHEN count_of_companies > 1 THEN 'MultiCompany User' ELSE 'Single' END FROM
(
SELECT EmailID, COUNT(companyID) AS count_of_companies
FROM #temp_company
GROUP BY EmailID
)t
答案 1 :(得分:0)
通过电子邮件统计公司和分区:
select email,
(case when count(companyid)over(partition by email) > 1
then 'multi-company-user'
else 'single-company-user'
end) as count_of_companies
from your_table
答案 2 :(得分:0)
试试这个
SELECT EMAIL
,CASE
WHEN COUNT(COMPANY_ID) > 1
THEN 'MULTI-COMPANY'
ELSE 'SINGLE-COMPANY'
END AS COUNT_OF_COMPANIES
FROM DATABASEA
GROUP BY EMAIL
ORDER BY COUNT_OF_COMPANIES DESC