6

我在发票和信用卡交易之间存在多对多关系,我试图将它们的总和映射在一起。思考这个问题的最好方法是将 TransactionInvoiceMap 想象成一个二分图。对于每个连接的子图,找到该子图中所有发票的总数和所有交易的总数。在我的查询中,我想返回为每个子图计算的值以及它们关联的事务 ID。关联交易的合计应相同。

更明确地说,给定以下交易/发票

Table: TransactionInvoiceMap
TransactionID  InvoiceID
1              1
2              2
3              2
3              3

Table: Transactions
TransactionID  Amount
1              $100
2              $75
3              $75

Table: Invoices
InvoiceID  Amount
1          $100
2          $100
3          $50

我想要的输出是

TransactionID  TotalAsscTransactions TotalAsscInvoiced
1              $100                  $100
2              $150                  $150
3              $150                  $150

请注意,发票 2 和 3 以及交易记录 2 和 3 是逻辑组的一部分。

这是一个显然有效但速度很慢的解决方案(简化,名称已更改)。我很难弄清楚如何优化它,但我认为这将涉及消除 TransactionInvoiceGrouping 中的子查询。随意提出一些完全不同的建议。

with TransactionInvoiceGrouping as (
    select 
        -- Need an identifier for each logical group of transactions/invoices, use
        -- one of the transaction ids for this.
        m.TransactionID,
        m.InvoiceID,
        min(m.TransactionID) over (partition by m.InvoiceID) as GroupingID
    from TransactionInvoiceMap m
)
select distinct
    g.TransactionID,
    istat.InvoiceSum as TotalAsscInvoiced,
    tstat.TransactionSum as TotalAsscTransactions
from TransactionInvoiceGrouping g
    cross apply (
        select sum(ii.Amount) as InvoiceSum
        from (select distinct InvoiceID, GroupingID from TransactionInvoiceGrouping) ig
            inner join Invoices ii on ig.InvoiceID = ii.InvoiceID
        where ig.GroupingID = g.GroupingID
    ) as istat
    cross apply (
        select sum(it.Amount) as TransactionSum
        from (select distinct TransactionID, GroupingID from TransactionInvoiceGrouping) ig
            left join Transactions it on ig.TransactionID = it.TransactionID
        where ig.GroupingID = g.GroupingID
        having sum(it.Amount) > 0
    ) as tstat
4

2 回答 2

2

我已经在递归 CTE中实现了该解决方案:

;with TranGroup as (
    select TransactionID
        , InvoiceID as NextInvoice
        , TransactionID as RelatedTransaction
        , cast(TransactionID as varchar(8000)) as TransactionChain
    from TransactionInvoiceMap
    union all
    select g.TransactionID
        , m1.InvoiceID
        , m.TransactionID
        , g.TransactionChain + ',' + cast(m.TransactionID as varchar(11))
    from TranGroup g
        join TransactionInvoiceMap m on g.NextInvoice = m.InvoiceID
        join TransactionInvoiceMap m1 on m.TransactionID = m1.TransactionID
    where ',' + g.TransactionChain + ',' not like '%,' + cast(m.TransactionID as varchar(11)) + ',%'
)
, RelatedTrans as (
    select distinct TransactionID, RelatedTransaction
    from TranGroup
)
, RelatedInv as (
    select distinct TransactionID, NextInvoice as RelatedInvoice
    from TranGroup
)
select TransactionID
    , (
        select sum(Amount)
        from Transactions
        where TransactionID in (
            select RelatedTransaction
            from RelatedTrans
            where TransactionID = t.TransactionID
        )
    ) as TotalAsscTransactions
    , (
        select sum(Amount)
        from Invoices
        where InvoiceID in (
            select RelatedInvoice
            from RelatedInv
            where TransactionID = t.TransactionID
        )
    ) as TotalAsscInvoiced
from Transactions t

可能有一些优化空间(包括我的对象命名!)但我相信我至少有一个正确的解决方案,它将收集所有可能的交易发票关系以包括在计算中。

我无法在此页面上获得现有解决方案以提供 OP 所需的输出,并且随着我添加更多测试数据,它们变得更加丑陋。我不确定 OP 发布的“慢”解决方案是否如所述正确。我很可能误解了这个问题。

附加信息:

我经常看到在处理大量数据时递归查询可能会很慢。也许这可能是另一个 SO 问题的主题。如果是这种情况,在 SQL 端尝试的事情可能是限制范围(添加where子句),索引基表,首先将 CTE 选择到临时表中,索引该临时表,为 CTE 考虑更好的停止条件...但首先是个人资料,当然。

于 2012-07-16T21:26:07.890 回答
0

如果我正确理解了这个问题,我认为您正在尝试找到每张发票的最小交易 ID,并且我已经使用排名功能来做同样的事情。

WITH TransactionInvoiceGrouping AS (
SELECT 
    -- Need an identifier for each logical group of transactions/invoices, use
    -- one of the transaction ids for this.
    m.TransactionID,
    m.InvoiceID,       
    ROW_NUMBER() OVER (PARTITION BY m.InvoiceID ORDER BY m.TransactionID ) AS recno
FROM TransactionInvoiceMap m
)
 SELECT 
g.TransactionID,
istat.InvoiceSum AS TotalAsscInvoiced,
tstat.TransactionSum AS TotalAsscTransactions
 FROM TransactionInvoiceGrouping g
CROSS APPLY(
    SELECT SUM(ii.Amount) AS InvoiceSum
    FROM TransactionInvoiceGrouping ig
        inner JOIN Invoices ii ON ig.InvoiceID = ii.InvoiceID
    WHERE ig.TransactionID = g.TransactionID
    AND ig.recno = 1
) AS istat
CROSS APPLY(
    SELECT sum(it.Amount) AS TransactionSum
    FROM TransactionInvoiceGrouping ig
        LEFT JOIN transactions it ON ig.TransactionID = it.TransactionID
    WHERE ig.TransactionID = g.TransactionID
    AND ig.recno = 1
    HAVING SUM(it.Amount) > 0
) AS tstat

WHERE g.recno = 1
于 2012-07-16T17:30:08.350 回答