0

私は3つのテーブルを持っています:保険証券、請求および売掛金

ポリシーごとにポリシー期間ごとに1行を返すクエリが必要です。クエリには、ポリシーの開始日と終了日、各期間の合計請求額、各期間の合計支払額と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を追加する必要がありましたが、このクエリは機能します。

3番目のテーブルを追加すると、乗数が得られます。クレームからのすべては、その期間の売掛金の行数で乗算され、AmountRecvdはその期間のクレームの行数で乗算されます。

クエリは次のとおりです。

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 に答える