如何加入多对多并保持相同的总金额

时间:2016-11-16 01:02:27

标签: tsql join sql-server-2012 sum

我有两个数据集。左数据集具有相同的QuoteID,PolicyNumber,但可以是不同的Year,Month和PaidLosses。 第二个数据集具有不同的QuoteID,不同的PolicyNumber年份和不同的Month,也可以是多个ClassCode。 我需要加入第一个数据集和第二个数据集并保持相同的PaidLosses。主要目标是每月保持相同的总付费。我知道它可能不是很正常,但那是老板想要看到的。

enter image description here

这是我到目前为止所尝试的:

select      
            cte1.PolicyNumber,
            AccidentYear,
            AccidentMonth,
            cte2.ClassCode,
    /*
        Using ROW_NUMBER() to check if it's the first record in the join and returns
        the PaidLosses value if so, otherwise it will display 0. The ORDER BY (SELECT 0) 
        is there just because I don't need the row number to be based on any explicit 
        order.
    */
            CASE
                WHEN ROW_NUMBER() OVER (PARTITION BY cte1.QuoteID, cte1.PolicyNumber ORDER BY (SELECT 0))=1 THEN cte1.PaidLosses 
                ELSE 0
            END  as PaidLosses
from        cte1 inner join cte2 on cte1.PolicyNumber=cte2.PolicyNumber AND cte1.QuoteID=cte2.QuoteID AND cte1.AccidentYear=cte2.LossYear
            AND cte1.AccidentMonth=cte2.LossMonth

但由于某种原因,它并没有取消一些政策。 理想情况下,我希望看到类似的东西: 在第一行有付的损失, 但是如果ClassCode重复相同的Policy,QuoteID,Year和Month则为0。

enter image description here

1 个答案:

答案 0 :(得分:1)

我认为你应该按cte1.AccidentYear, cte1.AccidentMonth进行分区。

CASE
WHEN ROW_NUMBER() OVER (PARTITION BY cte1.QuoteID, cte1.PolicyNumbe  cte2.LossYear, cte2.AccidentMonth ORDER BY (SELECT 0))=1 THEN cte1.PaidLosses 
ELSE 0
END  as PaidLosses.

结果将是:

QuoteId PolicyNumber    AccidentYear    AccidentMonth   ClassCode   

PaidLosses
191289  PACA1001776-0   2015    4   50228   26657
191289  PACA1001776-0   2015    4   67228   0
191289  PACA1001776-0   2015    9   50228   16718
191289  PACA1001776-0   2015    9   67228   0
191289  PACA1001776-0   2016    1   50228   3445
191289  PACA1001776-0   2016    1   67228   0

你需要的是什么?