使用CTE表计算域名

时间:2019-02-10 04:23:07

标签: sql-server-2008

我想计算要从结果中排除某些域名的电子邮件地址的域名,但是我的查询超时了,所以我尝试使用cte表,但不排除那些域名。

尝试1: 我尝试在没有cte表的情况下进行查询,由于正在进行大量计算,因此超时。

尝试2:我尝试使用CTE表,但查询并未排除CTE表中的那些域。因此,由于我不习惯使用CTE表,所以我不了解发生了什么。

客户表(这是一个虚拟数据集,实际上我有一个非常大的数据集)

EmailAddress

xyz@gmail.com
abc1@gmail.com
xyz@yahoo.com
cvbv@yahoo.com
123@comcast.net
cvx1@comcast.net
abv@comcast.net
cxt@msn.com
abcd@msn.com
3453w@att.net
weszt@att.net
wrt@live.com
adcs@live.com


Try 1:
  SELECT
    Domain = RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress))
    ,EmailCount = COUNT(DISTINCT c.EmailAddress)
FROM Customers c
WHERE
    LEN(c.EmailAddress) > 0
    AND RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress))NOT IN ('gmail.com'
            ,'yahoo.com'
            ,'comcast.net'
           )
GROUP BY RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress))
ORDER BY EmailCount DESC

TRY 2: With CTE

with cte_x as ( SELECT RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress)) AS DOMAIN
FROM Customers c
WHERE LEN(c.EmailAddress) > 0
 AND RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress)) NOT IN
   ('gmail.com'
            ,'yahoo.com'
            ,'comcast.net')) 
SELECT RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress)) AS DOMAIN_1 ,
 COUNT(DISTINCT c.EmailAddress) AS EmailCount
FROM Customers c
LEFT JOIN cte_x ON c.EmailAddress=cte_x.DOMAIN
WHERE cte_x.DOMAIN IS NULL
GROUP BY
RIGHT(c.EmailAddress, LEN(c.EmailAddress) - CHARINDEX('@', c.EmailAddress))

Result Coming:

Domain      count
gmail.com      2
yahoo.com       2
comcast.net     3
msn.com        2
att.net        2
live.com       2

Results Expected:

Domain      count
msn.com        2
att.net        2
live.com       2

0 个答案:

没有答案