简单的SQL查询

时间:2018-10-16 08:59:43

标签: sql tsql count

我有一个SQL脚本,并且希望根据不同的where子句进行多次计数,而不是像这样重复,有没有办法简化这一过程?

select
    UnverifiedEmails = 
        (
            select count(distinct c.ContactRef) 
            from ContactEmailAddressVerification c
            LEFT JOIN EmailAddressVerification e
            ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
            WHERE DateVerified IS NULL
        ),
    VerifiedEmails =
        (
            select count(distinct c.ContactRef) 
            from ContactEmailAddressVerification c
            LEFT JOIN EmailAddressVerification e
            ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
            WHERE DateVerified IS NOT NULL
        ),
    LastMonthVerified =
        (
            select count(distinct c.ContactRef) 
            from GDPR_ContactEmailAddressVerification c
            LEFT JOIN EmailAddressVerification e
            ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
            WHERE DateVerified IS NOT NULL
            AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
        ),
    LastMonthUnverified =
        (
            select count(distinct c.ContactRef) 
            from ContactEmailAddressVerification c
            LEFT JOIN EmailAddressVerification e
            ON e.EmailAddressVerificationID = c.EmailAddressVerificationID
            WHERE DateVerified IS NULL
            AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
        )

3 个答案:

答案 0 :(得分:4)

您可以使用单个SELECT语句进行汇总:

SELECT COUNT(DISTINCT CASE WHEN DateVerified IS NULL THEN c.ContactRef END) UnverifiedEmails,
       COUNT(DISTINCT CASE WHEN DateVerified IS NOT NULL THEN c.ContactRef END) VerifiedEmails,
       COUNT(DISTINCT CASE WHEN (DateVerified IS NOT NULL AND 
                                 DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
                                ) 
                           THEN c.ContactRef
             END) LastMonthVerified,
       COUNT(DISTINCT CASE WHEN (DateVerified IS NULL AND 
                                 DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
                                ) 
                           THEN c.ContactRef 
             END) LastMonthUnverified 
FROM ContactEmailAddressVerification c LEFT JOIN 
     EmailAddressVerification e
     ON e.EmailAddressVerificationID = c.EmailAddressVerificationID;

答案 1 :(得分:1)

不需要多个子查询的用例

select  count(distinct case when DateVerified IS NULL then  c.ContactRef end) UnverifiedEmails ,
        count(distinct case when DateVerified IS not NULL then  c.ContactRef end) VerifiedEmails,
        count(distinct case when DateVerified IS NOT NULL
                     AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
                     then  c.ContactRef end ) LastMonthVerified,
        count(distinct case when DateVerified IS NULL
                     AND DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, getdate()))
                     then  c.ContactRef end) LastMonthUnverified

                from ContactEmailAddressVerification c
                LEFT JOIN EmailAddressVerification e
                ON e.EmailAddressVerificationID = c.EmailAddressVerificationID

答案 2 :(得分:1)

您可以使用条件聚合:

SELECT
    COUNT(DISTINCT CASE WHEN DateVerified IS NULL
                        THEN c.ContactRef END) AS UnverifiedEmails,
    COUNT(DISTINCT CASE WHEN DateVerified IS NOT NULL
                        THEN c.ContactRef END) AS VerifiedEmails,
    COUNT(DISTINCT CASE WHEN DateVerified IS NOT NULL AND
        DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, GETDATE()))
                        THEN c.ContactRef END) AS LastMonthVerified,
    COUNT(DISTINCT CASE WHEN DateVerified IS NULL AND
        DATEPART(m, e.DateAdded) = DATEPART(m, DATEADD(m, -1, GETDATE()))
                        THEN c.ContactRef END) AS LastMonthUnverified
FROM ContactEmailAddressVerification c
LEFT JOIN EmailAddressVerification e
    ON e.EmailAddressVerificationID = c.EmailAddressVerificationID;

这里的想法是对联接的表进行一次传递,然后根据原始查询的每个WHERE子句中的逻辑有条件地进行计数/求和。