0

我有 3 张表:保险单、索赔和应收账款

我需要一个查询,该查询将在每个策略的每个策略周期返回一行。查询需要包括保单开始和结束日期、每个时期的总索赔、每个时期的总支付和 O/S 以及每个时期的总收到金额。

除了应收账款,我什么都做了。当这被引入查询时,所有内容都乘以该表中的行数。

这是我的数据

政策

PolNo   Version   TransType   InceptionDate   RenewalDate
0021          0   New         01/01/2008      01/01/2009
0021          1   MTA         01/01/2008      01/01/2009
0021          2   MTA         01/01/2008      01/01/2009
0021          3   Renewal     01/01/2009      01/01/2010

索赔

PolNo   ClaimNo   ClaimDate      Paid    Outstanding
0021    0001      01/05/2008   300.00        -100.00
0021    0002      01/06/2008   500.00         200.00
0021    0003      01/07/2008   200.00         300.00
0021    0004      01/08/2008   800.00           0.00
0021    0005      01/02/2009     0.00           0.00
0021    0006      01/10/2009     0.00        1000.00

应收账款

PolNo   Version   RecvdDate    Amount
0021          0   02/01/2008   150.00
0021          0   01/02/2008   150.00
0021          0   01/03/2008   150.00
0021          0   01/04/2008   150.00
0021          0   01/05/2008   150.00
0021          0   01/06/2008   150.00
0021          0   01/07/2008   150.00
0021          0   01/08/2008   150.00
0021          2   01/09/2008   150.00
0021          2   01/10/2008   150.00
0021          3   02/01/2009   500.00
0021          3   01/02/2009   500.00

这是我的工作查询

select distinct(a.InceptionDate) as InceptionDate, a.RenewalDate as RenewalDate,
count(b.ClaimNo) as ClaimCount, sum(b.Paid) as TotPaid, sum(b.Outstanding) as TotalO/S
from Policies a, Claims b
where a.PolNo='0021'
and a.PolNo=b.PolNo
and b.ClaimDate between a.InceptionDate and a.RenewalDate
and a.TransType in ('New','Renewal')
group by a.InceptionDate, a.RenewalDate

结果:

InceptionDate   RenewalDate   ClaimCount      TotPaid       TotalO/S
01/01/2008      01/01/2009        4           1800.00       400.00
01/01/2009      01/01/2010        2           0             1000.00

当我获得索赔数据的乘数时,我必须将 TransType 添加到查询中,但此查询有效。

现在,当我添加第三个表时,我得到了一个乘数。Claims 中的所有内容都乘以该期间应收账款中的行数,而 AmountRecvd 乘以该期间 Claims 中的行数。

这是查询:

select distinct(a.InceptionDate) as InceptionDate, a.RenewalDate as RenewalDate,
count(b.ClaimNo) as ClaimCount, sum(b.Paid) as TotPaid, sum(b.Outstanding) asTotalO/S,
sum(c.Amount) as RecvdAmount
from Policies a, Claims b, Receivables c
where a.PolNo='0021'
and a.PolNo=b.PolNo
and a.PolNo=c.PolNo
and b.ClaimDate between a.InceptionDate and a.RenewalDate
and c.RecvdDate between a.InceptionDate and a.RenewalDate
and a.TransType in ('New','Renewal')
group by a.InceptionDate, a.RenewalDate

结果

InceptionDate   RenewalDate   ClaimCount   TotPaid   TotalO/S   AmountRecvd
01/01/2008      01/01/2009       40         18000       4000       6000.00
01/01/2009      01/01/2010       20             0      10000       2000.00

我看不到可以将应收账款与索赔结合起来。我尝试在 PolNo 上加入他们,但结果相同。

谁能看到解决方案?

干杯

更新了此查询不会乘以 ClaimsCount,但 AmountRevd 仍乘以 Claims(为简单起见,删除了 TotPaid):

SELECT
    p.InceptionDate,
    p.RenewalDate,
    sum(c1.ClaimCount) AS ClaimsCount,
    sum(r1.TotRecvd) AS AmountRecvd
FROM Policies p
LEFT JOIN (SELECT 
        p1.InceptionDate, 
        p1.RenewalDate, 
        c.Polno, 
        c.ClaimDate,
        COUNT(c.SubCount) AS ClaimCount
    FROM Policies p1 
    INNER JOIN (SELECT 
            PolNo,
            ClaimDate,
            Count(*) as SubCount
        FROM Claims 
        GROUP BY PolNo, ClaimDate) c
        ON p1.PolNo=c.PolNo
    WHERE c.ClaimDate BETWEEN p1.InceptionDate AND p1.RenewalDate
    AND p1.TransType IN ('New','Renewal')
    GROUP BY p1.InceptionDate, p1.RenewalDate, c.Polno, c.ClaimDate) c1
    ON p.PolNo=c1.PolNo
LEFT JOIN (SELECT 
        p2.InceptionDate, 
        p2.RenewalDate, 
        r.Polno,
        SUM(r.SubTot) AS TotalRecvd
    FROM Policies p2
    INNER JOIN (SELECT
            PolNo,
            RecvdDate,
            SUM(Amount) as SubTot
        FROM Receivables
        GROUP BY Polno,RecvdDate) r
        ON p2.PolNo=r.PolNo
    WHERE r.RecvdDate BETWEEN p2.InceptionDate AND p2.RenewalDate
    AND p2.TransType IN ('New','Renewal')
    GROUPBY p2.InceptionDate, p2.RenewalDate, r.Polno) r1
    ON p.PolNo=r1.PolNo
WHERE p.PolNo = '0021'
AND p.TransType IN ('New','Renewal')
AND p.PolNo = c1.PolNo
AND p.PolNo = r1.PolNo
AND p.InceptionDate = c1.InceptionDate
AND p.RenewalDate = c1.RenewalDate
AND p.InceptionDate = r1.InceptionDate
AND p.RenewalDate = r1.RenewalDate
GROUP BY p.InceptionDate, p.RenewalDate
4

1 回答 1

1
-- Query for the periods:
WITH Periods AS (
    SELECT DISTINCT
        PolNo, InceptionDate, RenewalDate
    FROM Policies
    WHERE TransType in ('New','Renewal')
),

-- Query for the claims
PeriodClaims AS (
    SELECT
        p.PolNo, p.InceptionDate, p.RenewalDate,
        COUNT(*) AS CountClaims,
        SUM(c.Paid) AS PaidClaims,
        SUM(c.Outstanding) AS OutstandingClaims
    FROM Periods p
    INNER JOIN Claims c ON c.PolNo = p.PolNo
    WHERE c.ClaimDate BETWEEN p.InceptionDate AND p.RenewalDate
    GROUP BY p.PolNo, p.InceptionDate, p.RenewalDate
),

-- Query for the receivables
PeriodRecieved AS (
    SELECT
        p.PolNo, p.InceptionDate, p.RenewalDate,
        COUNT(*) AS CountReceived,
        SUM(r.Amount) AS AmountReceived
    FROM Periods p
    INNER JOIN Receivables r ON r.PolNo = p.PolNo
    WHERE r.RecvdDate BETWEEN p.InceptionDate AND p.RenewalDate
    GROUP BY p.PolNo, p.InceptionDate, p.RenewalDate
)

-- All together now
SELECT 
    p.PolNo, p.InceptionDate, p.RenewalDate,
    c.CountClaims, c.PaidClaims, c.OutstandingClaims,
    r.CountReceived, r.AmountReceived
FROM Periods p
LEFT JOIN PeriodClaims c
    ON c.PolNo = p.PolNo
    AND c.InceptionDate = p.InceptionDate
    AND c.RenewalDate = p.RenewalDate
LEFT JOIN PeriodRecieved r
    ON r.PolNo = p.PolNo
    AND r.InceptionDate = p.InceptionDate
    AND r.RenewalDate = p.RenewalDate

或者,没有 CTE:

SELECT 
    p.PolNo, p.InceptionDate, p.RenewalDate,
    c.CountClaims, c.PaidClaims, c.OutstandingClaims,
    r.CountReceived, r.AmountReceived
FROM (
    -- Query for the periods:
    SELECT DISTINCT
        PolNo, InceptionDate, RenewalDate
    FROM Policies
    WHERE TransType in ('New','Renewal')
) p
LEFT JOIN (
    -- Query for the claims
    SELECT
        p.PolNo, p.InceptionDate, p.RenewalDate,
        COUNT(*) AS CountClaims,
        SUM(c.Paid) AS PaidClaims,
        SUM(c.Outstanding) AS OutstandingClaims
    FROM (
        -- Query for the periods:
        SELECT DISTINCT
            PolNo, InceptionDate, RenewalDate
        FROM Policies
        WHERE TransType in ('New','Renewal')
    ) p
    INNER JOIN Claims c ON c.PolNo = p.PolNo
    WHERE c.ClaimDate BETWEEN p.InceptionDate AND p.RenewalDate
    GROUP BY p.PolNo, p.InceptionDate, p.RenewalDate
) c
    ON c.PolNo = p.PolNo
    AND c.InceptionDate = p.InceptionDate
    AND c.RenewalDate = p.RenewalDate
LEFT JOIN (
    -- Query for the receivables
    SELECT
        p.PolNo, p.InceptionDate, p.RenewalDate,
        COUNT(*) AS CountReceived,
        SUM(r.Amount) AS AmountReceived
    FROM (
        -- Query for the periods:
        SELECT DISTINCT
            PolNo, InceptionDate, RenewalDate
        FROM Policies
        WHERE TransType in ('New','Renewal')
    ) p
    INNER JOIN Receivables r ON r.PolNo = p.PolNo
    WHERE r.RecvdDate BETWEEN p.InceptionDate AND p.RenewalDate
    GROUP BY p.PolNo, p.InceptionDate, p.RenewalDate
) r
    ON r.PolNo = p.PolNo
    AND r.InceptionDate = p.InceptionDate
    AND r.RenewalDate = p.RenewalDate

输出(转置):

PolNo                       21           21
InceptionDate       2008-01-01   2009-01-01
RenewalDate         2009-01-01   2010-01-01
CountClaims                  4            2
PaidClaims             1800.00         0.00
OutstandingClaims       400.00      1000.00
CountReceived               10            2
AmountReceived         1500.00      1000.00
于 2011-08-17T22:12:16.753 回答