我正在尝试确定“第一次捐赠者”,他们在FY2015中做出了贡献,但以前从未做出过贡献。我还需要忽略出于某种原因进行捐赠的用户(appealCode)。
下面是我的表中的一些字段以及需要限制的信息的示例。
**FundLedger**
Account ID EntryAmount GiftReceivedDt AppealCode
1000 $500 7/1/2014 1
1000 $500 2/2/2002 2
2000 $25 8/1/2014 1
2000 $25 9/1/2014 1
3000 $100 10/1/2014 1
4000 $1,000 11/1/2014 2
**ConstituentTotals**
ConstituentID FirstTransactionDate LastTransactionDate CashAmount
1000 2/2/2002 1/1/2014 $1,000
2000 3/1/2014 4/1/2014 $50
3000 5/1/2014 5/1/2014 $100
4000 11/1/2014 11/1/2014 $1,000 我需要的是找出在2014年6月1日到今天之间从未捐赠过的选民的数量,而且礼物也没有赠送给AppealCode 2。
所以我需要从样本信息中得到的数字是'2‘。
**Information Needed**
ConstituentID CashAmount FirstTransactionDate LastTransactionDate AppealCode
2000 $50 3/1/2014 4/1/2014 1
3000 $100 5/1/2014 5/1/2014 1到目前为止,如果我忽略AppealCode,我可以获得捐赠者的人数,或者我可以获得AppealCode限制,但我可以获得捐赠者的所有交易。
目前,在此阶段,它提取计数77,000次,分别对应于Ledger中的每个条目。
'SELECT
Number_Of_New_Donors = ( SELECT COUNT(a.ConstituentID)
From dbo.FundConstituentTotals a
RIGHT JOIN dbo.FundLedger b
ON a.ConstituentID = b.AccountID
WHERE (a.FirstTransactionDT between '6/1/2014' and '5/31/2015'
AND a.CashAmount > '0'
AND a.GivingYear = '2015'
AND A.GivingYear !< '2015')
AND (b.GiftReceivedDt between '6/1/2014' and '5/31/2015'
AND b.RecordTypeID != '0'
AND b.RecordTypeID != '-1'
AND b.RecordTypeID != '2'))
FROM FundConstituentTotals'建议的响应结果:
ConstituentID FundConstituentTotalID ConstituentID GivingYear PledgeAmount CashAmount NonCashAmount FirstTrans
49427 77314 49427 2015 0 25 0 1/13/2015
49427 77314 49427 2015 0 25 0 1/13/2015
49427 77314 49427 2015 0 25 0 1/13/2015
49427 77314 49427 2015 0 25 0 1/13/2015刚刚发现数据是准确的,FirstTransactionDate不提供第一笔交易的日期,只提供交易开始过帐到账本的日期(过去有人搞砸了)。我将不得不在日期之间使用GiftReceivedDate,并找到一种方法来删除那些在2014年前有日期的人。
发布于 2015-01-19 23:46:26
SELECT Number_Of_New_Donors = ( SELECT COUNT(a.ConstituentID)
From dbo.FundConstituentTotals a
LEFT JOIN dbo.FundLedger b
ON a.ConstituentID = b.AccountID
AND a.LastTransactionDate = b.GiftReceivedDt
WHERE a.FirstTransactionDT between '2015-01-01' and '2015-05-31'
AND b.AppealCode != 2)这看起来就是你想要的。FirstTransaction是今年的(因为它是第一年,不可能有以前的),并且呼吁代码不是2。你的其余代码试图做什么?
https://stackoverflow.com/questions/28027997
复制相似问题