我有两张桌子。第一个,LeadSales
跟踪我们卖给经纪人的线索。第二个,LeadSalesCreditActions
保留每笔销售的所有交易的历史记录,包括退款请求。
这是一个示例(为了便于查看,发布了一些列并缩短了名称):
LeadSales
LeadSalesID LeadID SoldToContactID Amount CurrentCreditStatus
-------------------------------------------------------------------------
190 3241 45 3.50 0 (new)
191 3398 45 3.75 1 (refund requested)
192 3410 45 3.95 2 (refund granted)
因此,联系人45购买了3个潜在客户。他要求对其中两个人提供信贷,并且一个信用额已被授予,而其中一个信用额仍在等待中。
LeadSalesCreditActions
ID CreatedByContactID LeadSalesID Comment CreditStatus
------------------------------------------------------------------------
250 NULL 190 NULL 0 (new)
251 NULL 191 NULL 0 (new)
252 45 191 "Dude, no good" 1 (refund requested)
253 NULL 192 NULL 0 (new)
254 45 192 "Dudes, bad data" 1 (refund requested)
255 1 192 "Sorry about that" 2 (refund granted)
同样,LeadSalesCreditActions
会记录每笔交易以防止销售。因此,有一个记录显示何时出售一个潜在客户,另一个如果经纪人要求提供信贷,另一个当我们授予或拒绝信用额时。
现在,我们希望查看仍在等待的每个当前信用请求的报告。意思是,我们尚未批准或拒绝该请求。
我有一个将两个表连接在一起的报表查询。这工作正常,但缺少一些东西......
var creditActions = (get List<LeadSalesCreditActions> where Max CreditStatus = 1)
var leadSales = (get List<LeadSales> where Max CurrentCreditStatus = 1)
var joined = from leadSale in leadSales
join creditAction in creditActions on leadSale.LeadSalesID equals creditAction.LeadSalesID into crs
from creditAction in crs.DefaultIfEmpty()
select new CreditSearchItemModel
{
LeadSaledID = leadSale.LeadSalesID,
Lead = leadSale.Lead,
Contact = leadSale.Contact,
Comment = creditAction.Comment,
LeadSalesCreditReason = creditAction.LeadSalesCreditReason,
LeadSalesType = leadSale.LeadSalesType,
DateCreated = creditAction.DateCreated,
CountCreditsRequested = ??????
CountLeadsPurchased = ?????
};
如果我遗漏了至少两个Count
项,那么一切正常。我得到了我想要的结果。除了......我遇到的问题是如何获得两个计数:CountCreditsRequested
和CountLeadsPurchased
。在联系人45的情况下,这些数字将是:
CountCreditsRequested = 2
CountLeadsPurchased = 3
所以最终报告看起来像这样:
LeadID Broker Requests Reason Comment
--------------------------------------------------------------------------
3398 John Doe 2/3 Bad Contact Info "Dude, no good" [Refund Button] [Deny Button]
但这仅适用于Contact 45.此查询不是特定于联系人,我们需要一组所有联系人的结果。当然,无论他在报告中出现多少次,“结果”列中的数字对于每个经纪人都是相同的。
我发现其他一些线程有点接近,like this一个,但没有一个让我一直到那里。
我希望所有这一切都有道理!谢谢!