使用过滤器在SQL中查找重复项

时间:2018-06-21 16:00:48

标签: sql

除了查找重复项外,如何使用“以及其中的primarysmtpaddress如“ %@sub.domain.com””之类的内容进行过滤

SELECT y.primarysmtpaddress, y.phonenumber
FROM UMEnabledAccounts y
    INNER JOIN (SELECT
                    phonenumber, COUNT(*) AS CountOf
                    FROM UMEnabledAccounts
                    GROUP BY phonenumber
                    HAVING COUNT(*)>1 
                )  dt ON y.phonenumber=dt.phonenumber order by PhoneNumber asc

3 个答案:

答案 0 :(得分:0)

在分组依据之前放置一个where子句。这将仅对过滤的行执行分组。

SELECT phonenumber, COUNT(*) AS CountOf
FROM UMEnabledAccounts
where primarysmtpaddress like '%@sub.domain.com'

答案 1 :(得分:0)

根据@vendettamit,完整查询将为

SELECT y.primarysmtpaddress, y.phonenumber
    FROM UMEnabledAccounts y
        INNER JOIN (SELECT
                        phonenumber, COUNT(*) AS CountOf
                        FROM UMEnabledAccounts
                        where  primarysmtpaddress like '%@sub.domain.com'
                        GROUP BY phonenumber
                        HAVING COUNT(*)>1 
                    )  dt ON y.phonenumber=dt.phonenumber order by PhoneNumber asc

答案 2 :(得分:0)

SELECT y.primarysmtpaddress, y.phonenumber
FROM UMEnabledAccounts y
INNER JOIN (SELECT
                phonenumber, COUNT(*) AS CountOf
                FROM UMEnabledAccounts
                GROUP BY phonenumber
                HAVING COUNT(*)>1 
            )  dt 
          ON y.phonenumber=dt.phonenumber 
          WHERE primarysmtpaddress like ‘%@sub.domain.com’
          order by PhoneNumber asc;

上面的查询将创建联接,然后在新形成的行上实现where子句。