【问题标题】:How can I make this SQL Server script dynamic through the years?多年来,如何使这个 SQL Server 脚本动态化?
【发布时间】:2019-05-22 00:10:48
【问题描述】:

我有确定每年“失效”捐赠者数量的代码。我每年都会结合使用工会的捐助者失效。我希望这些数据每年自动更新而不添加联合。有没有办法让岁月变得充满活力?

我只使用了当前的静态脚本,自 2013 年以来我必须拉出所有失效的捐助者。提供的脚本显示 2017-2019。

--Donated 2018, Not 2019
SELECT DISTINCT 
    PY.OppFiscalYear, COUNT(DISTINCT PY.AccountId)
FROM
    (SELECT DISTINCT PY.AccountId, PY.OppFiscalYear
     FROM reporting.AllGifts PY
     WHERE PY.OppFiscalYear = '2018'
       AND PY.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
       AND PY.AccountRecordType = 'Household'
       AND PY.OppStage = 'Paid') PY
LEFT JOIN
    (SELECT DISTINCT Y.AccountId
     FROM reporting.AllGifts Y
     WHERE Y.OppFiscalYear = '2019'
       AND Y.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
       AND Y.AccountRecordType = 'Household'
       AND Y.OppStage = 'Paid') Y ON PY.ACCOUNTID = Y.ACCOUNTID
WHERE 
    Y.AccountId IS NULL
GROUP BY 
    PY.OppFiscalYear

UNION

--Donated 2017, Not 2018
SELECT DISTINCT 
    PY.OppFiscalYear, COUNT(DISTINCT PY.AccountId)
FROM
    (SELECT DISTINCT PY.AccountId, PY.OppFiscalYear
     FROM reporting.AllGifts PY
     WHERE PY.OppFiscalYear = '2017'
       AND PY.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
       AND PY.AccountRecordType = 'Household'
       AND PY.OppStage = 'Paid') PY
LEFT JOIN
    (SELECT DISTINCT Y.AccountId
     FROM reporting.AllGifts Y
     WHERE Y.OppFiscalYear = '2018'
       AND Y.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
       AND Y.AccountRecordType = 'Household'
       AND Y.OppStage = 'Paid') Y ON PY.ACCOUNTID = Y.ACCOUNTID
WHERE 
    Y.AccountId IS NULL
GROUP BY 
    PY.OppFiscalYear

我希望得到与目前相同的输出,但我希望它能够自动包含每天添加的任何新捐款。如果我继续使用当前的方法,则需要使用并集添加到顶部的新脚本是 2019 年和 2020 年。

【问题讨论】:

    标签: sql sql-server dynamic ssms


    【解决方案1】:

    不确定这将如何表现取决于您的数据集的大小,但相关的子查询应该会得到您想要的结果:

    SELECT PY.OppFiscalYear, COUNT(DISTINCT PY.AccountId)
    FROM reporting.AllGifts PY
    WHERE 
    PY.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
            AND PY.AccountRecordType= 'Household'
            AND PY.OppStage = 'Paid'
    AND NOT EXISTS
        (SELECT 1 FROM  reporting.AllGifts Y
    WHERE 
    Y.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
            AND Y.AccountRecordType= 'Household'
            AND Y.OppStage = 'Paid'
    AND Y.OppFiscalYear = PY.OppFiscalYear + 1
    AND Y.AccountId = PY.AccountId)
    AND OppFiscalYear <> (SELECT MAX(OppFiscalYear) FROM reporting.AllGift) -- Can't be lapsed from the latest year!
    GROUP BY OppFiscalYear
    

    【讨论】:

    • 是的!这产生了与我冗长的“联合”脚本相同的结果。谢谢!我已将此标记为正确答案。
    【解决方案2】:

    基本聚合不起作用吗?我想这就是你所追求的。

    SELECT PY.OppFiscalYear
        , COUNT(DISTINCT PY.AccountId)
    FROM reporting.AllGifts PY
    WHERE PY.OpportunityRecordType IN ('Classy', 'Donation', 'Pledge')
        AND PY.AccountRecordType= 'Household'
        AND PY.OppStage = 'Paid'
    group by PY.OppFiscalYear
    

    【讨论】:

    • 不幸的是,这个脚本没有解决失效的捐赠者,而只是每年有多少捐赠者。谢谢。
    • 好吧,我基本上是在猜测。下次如果您可以发布示例数据和该示例数据的所需输出,将会有很大帮助。
    猜你喜欢
    • 1970-01-01
    • 2012-04-15
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 2021-05-19
    • 1970-01-01
    相关资源
    最近更新 更多