Sql 多连接乘数
我有三张表:保险单、索赔和应收账款Sql 多连接乘数,sql,join,db2,cartesian-product,Sql,Join,Db2,Cartesian Product,我有三张表:保险单、索赔和应收账款 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
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
我需要一个查询,该查询将在每个策略的每个策略期间返回一行。查询需要包括保单开始和结束日期、每个期间的索赔总额、每个期间的支付总额和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添加到查询中,因为我要获得索赔数据的乘数,但是这个查询可以工作
现在,当我添加第三个表时,我得到了一个乘数。索赔中的所有内容乘以该期间应收账款中的行数,AMOUNTECVD乘以该期间索赔中的行数
以下是查询:
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
我看不出有可能将应收账款与债权结合起来。我试着在波尔诺加入他们,但结果是一样的
有人能找到解决办法吗
干杯
已更新此查询不包含多重索赔,但AMONTREVD仍与索赔相乘(为简单起见,删除了TotPaid):
或者,如果没有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
输出(转置):
谢谢你的编辑。任何关于如何在此处以可读格式获取表数据的提示都将不胜感激。谢谢MizardX。我得研究一下你的asnwer。所以您使用的是common table Expression。令人沮丧的是,我们没有权利在我工作的地方使用这些工具,所以它对我没有任何帮助。有其他选择吗?你可以嵌入表格。您不是从句点p编写
,而是从(选择…)p
编写。可读性较差,但应给出相同的结果。OK。我已经试过你建议的方法了。我已经设法改进了它,这样ClaimCount就不会被应收账款中的行所重复。然而,AMOUNTECVD仍在乘以权利要求书中的行数。我已经在上面添加了我当前的工作。在我的原始版本中有两个复制/粘贴错误。我更正了它们,同时创建了一个非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