Multiple Join Multipliers - sql

I have 3 tables: Insurance Policies, Claims and Receivables
I need a query that will return one row per policy period per policy. The query needs to include the policy start and end date, total claims for each period, total paid and O/S for each period, and total amount received for each period.
I've managed to do everything but the recievables. When this is introduced to the query, everything is multiplied by the number of rows in that table.
Here is my data
Policies
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
Claims
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
Receivables
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
Here is my working query
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
Result:
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
I had to add TransType to the query as I was getting a multiplier for claims data, but this query works.
Now when I add the third table, I get a multiplier. Everything from Claims is multiplied by the number or rows in Receivables for the period, and AmountRecvd is multiplied by the number of rows in Claims for the period.
Here is the query:
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
Result
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
I cant see that it is possible to join Receivables with Claims. I tried joining them on PolNo but had the same result.
Can anyone see a solution?
Cheers
Updated This query doesn't muliply ClaimsCount, but AmountRevd still multiplying by Claims (TotPaid removed for simplicity):
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

-- 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
Or, without 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
Output (transposed):
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

Related

JOIN Multiple Tables with SUM

I need help with the scenario below.
order table
orderId orderAmount
10001 1000.00
10002 2000.00
10003 3000.00
10004 1000.00
10005 1000.00
transfer table
transferId orderId transferAmount
9001 10001 100.00
9002 10001 200.00
9003 10001 25.00
9004 10002 250.00
9005 10002 450.00
9006 10004 100.00
returns table
returnId orderId returnAmount
8001 10001 450.00
8002 10001 50.00
8003 10002 245.00
8004 10003 100.00
Result Needed
/*include all orders from orders table, even if no transfers or returns*/
orderId transfers returns
10001 325.00 500.00 /*sum of transfers and returns per orderId from respective tables*/
10002 700.00 245.00
10003 0.00 100.00
10004 100.00 0.00
10005 0.00 0.00 /*use zero whenever no rows for orderId in respective table*/
How can I implement the above in single query?
This is tricky. I think the easiest way is union all. If I have the arithmetic correct, you want to add the order amount to transfers but not returns:
select orderId,
sum(transfers) as transfers,
sum(returns) as returns
from ((select orderId, Amount, 0 as transfers, 0 as returns
from orders o
) union all
(select orderId, 0, transferAmount, 0
from transfers
) union all
(select orderId, 0, 0, returnAmount
from transfers
)
) otr
group by orderId;
You can just select all the order lines.
For each order, you subquery the tables linked by the OrderId.
SELECT o.orderId
, (SELECT sum(TransferAmount)
FROM Transaction t
WHERE t.OrderId = o.OrderId) 'Transaction'
, (SELECT sum(r.ReturnAmount)
FROM Result r
WHERE r.OrderId = o.OrderId) 'Return'
FROM Order o
I would use derived tables, something like (Apologies if the syntax is a little off, not tested):
SELECT O.OrderID,COALESCE(T.Total,0) AS [Transfer Total],COALESCE(R.Total,0) AS [Return Total]
FROM Orders AS O
LEFT JOIN
(
SELECT T.OrderID,SUM(T.TransferAmount) [Total]
FROM Transfer_Table AS T
GROUP BY T.OrderID
) AS T ON O.OrderID = T.OrderID
LEFT JOIN
(
SELECT R.OrderID,SUM(R.ReturnAmount) [Total]
FROM Return_Table AS R
GROUP BY R.OrderId
) AS R ON R.OrderId = O.OrderID

Joining multiple tables with filter

I got multiple tables and i'm only using 3 for testing. I don't know if this is the right approach.
AccntTbl
id accnt amount date
---------------------------------
1 xxx 10.00 1/1/2016
2 yyy 20.00 1/1/2016
3 zzz 30.00 1/1/2016
SupplyTbl
id accnt supply date
-------------------------------
1 xxx 5.00 1/10/2016
1 xxx 5.00 2/14/2016
IssuedTbl
id accnt issued date
---------------------------------
1 xxx 3.00 1/9/2016
1 xxx 2.00 2/1/2016
and the result will be like this if filter to JANUARY
id accnt amount issued balance
--------------------------------------------
1 xxx 15.00 3.00 12.00
2 yyy 20.00 0.00 20.00
3 zzz 30.00 0.00 30.00
and if FEBRUARY
id accnt amount issued balance
---------------------------------------------
1 xxx 20.00 5.00 15.00
2 yyy 20.00 0.00 0.00
3 zzz 30.00 0.00 0.00
and here's my query that i've come up with
SELECT accnttbl.id,
accnttbl.accnt,
Sum(accnttbl.amount)
+ Sum(SupplyTbl.supply) AS amount,
issuedtbl.issued
FROM accnttbl
LEFT JOIN (SELECT id,
Sum(supply) AS supply,
date
FROM supplytbl
GROUP BY id,
date) AS SupplyTbl
ON accnttbl.id = SupplyTbl.id
AND Month(SupplyTbl.date) BETWEEN 1 AND 2
LEFT JOIN (SELECT id. Sum(issued) AS issued,
date
FROM issuedtbl
GROUP BY id,
date)
ON accnttbl.id = issuedtbl.id
AND Month(issuedtbl.date) BETWEEN 1 AND 2
WHERE Year(accnttbl.date) = 2016
GROUP BY accnttbl.id,
accnttbl.accnt
and i kinda mess up..glad for any help :)
the result be like this..in JANUARY
id accnt amount issued
1 xxx 30.00 10.00
2 yyy 20.00 0.00
and in FEBRUARY
1 xxx 60.00 20.00
2 yyy 20.00 0.00
with the value that i been given..it just..it doubled the amount on its own..the result are wrong..why is that ?
Try as below :
SELECT AccntTbl.id, AccntTbl.accnt
, SUM(AccntTbl.amount) + SUM(SupplyTbl.supply) as amount
, SUM(IssuedTbl.issued) as issued
, (SUM(amount) - SUM(issued)) as balance
FROM AccntTbl LEFT JOIN
(SELECT id, COALESCE(SUM(supply), 0) AS supply, date FROM SupplyTbl
GROUP By id, date) AS SupplyTbl
ON AccntTbl.id = SupplyTbl.id AND (MONTH(SupplyTbl.date) BETWEEN
1 AND 2)
LEFT JOIN
(SELECT id, COALESCE(SUM(issued),0) as issued, date from IssuedTbl
GROUP By id, date )
ON AccntTbl.id = IssuedTbl.id AND (MONTH(IssuedTbl.date) BETWEEN
1 AND 2)
WHERE (YEAR(AccntTbl.date) = 2016)
GROUP By AccntTbl.id, AccntTbl.accnt
;
In sql anything added with NULL is NULL . So in your subquery if you are not using COALESCE, you are adding your value with NULL, hence you are not getting your desired result.

joining multiple tables with filter by date

i got 3 tables..how do i join these tables with filter by date on each table
MainTbl
id accnt amount date
---------------------------------
1 xxx 10.00 1/1/2016
2 yyy 20.00 1/1/2016
3 zzz 30.00 1/1/2016
SupplyTbl - this table would add the value of supply to the amount in the MainTbl
id accnt supply date
-------------------------------
1 xxx 5.00 1/10/2016
1 xxx 5.00 2/14/2016
2 yyy 10.00 1/20/2016
IssuedTbl
id accnt issued dateIssue
------------------------------------------
1 xxx 5.00 1/10/2016
1 xxx 5.00 2/14/2016
2 yyy 10.00 2/15/2016
now i want to join these tables..with date range..if i filter the date to JANUARY it would result something like this
id accnt amount issued
-----------------------------
1 xxx 15.00 5.00
2 yyy 30.00 0.00
3 zzz 30.00 0.00
and when i filter to FEBRUARY..it will combine both from JANUARY to FEBRUARY
id accnt amount issued
-----------------------------
1 xxx 20.00 10.00
2 yyy 40.00 10.00
3 zzz 30.00 0.00
i'm struggling with this since the other night..glad with any help..tnx in advance :)
Try something like this
SELECT M.id,
M.accnt,
amount = M.amount + Isnull(s.supply, 0),
issued = Isnull(I.issued, 0)
FROM maintbl M
LEFT OUTER JOIN (SELECT id,
Sum(supply) AS supply
FROM supplytbl
WHERE Month(dates) = 1 -- Month filter for Jan - feb Month(dates) in (1,2)
GROUP BY id) S
ON s.id = M.id
LEFT OUTER JOIN (SELECT id,
Sum(issued) AS issued
FROM issuedtbl
WHERE Month(dateissue) = 1 -- Month filter
GROUP BY id) I
ON I.id = M.id
SQL FIDDLE DEMO

Zero Fill Data for Missing Dates

I have two tables:
Cust Sales Week
123 4 1/8/2015
123 3 1/22/2015
234 4 1/1/2015
.
Week
1/1/2015
1/8/2015
1/15/2015
1/22/2015
I want to combine them so that every Cust has every date and where there are no Sales it is filled with 0.
Cust Sales Week
123 4 1/1/2015
123 0 1/8/2015
123 0 1/15/2015
123 3 1/22/2015
234 4 1/1/2015
234 0 1/8/2015
234 0 1/15/2015
234 0 1/22/2015
Is there a way I can 'select distinct(Cust)' and join them somehow?
First, generate the rows you want using a cross join. Then bring in the data you want using a left join:
select c.cust, w.week, coalesce(t.sales, 0) as sales
from weeks w cross join
(select distinct cust from t) c left join
t
on t.cust = c.cust and t.week = w.week;
You can left join on the dates table and use isnull on the sales column. Use an equivalent of isnull in Netezza.
select t1.cust, isnull(t1.sales,0), t2.week
from daystable2 t2 left join salestable1 t1 on t1.week = t2.week
I think this will do the trick
SELECT week, cust, COALESCE(sales, 0)
FROM week_tbl a LEFT JOIN cust_table b
ON a.week = b.week

How get data from 3 tables?

I have 3 tables
collecton, paymentdata, payment
Payment table is computed table and it has only one product data
So, rcvamt and restamt get from payment table
I have following data
Collection:
id(PK) clientid company Client product total note
1 2001 Company1 Client1 Product1 50000 note1
2 2002 Company2 Client2 Product2 60000 note2
3 2003 Company3 Client3 Product3 70000 note3
PaymentData:
wid(PK)wcid(FK) clientid product rcvamt restamt rcvdate nxtdate Note
1 1 2001 Product1 500 49500 10-1-2015 11-2-2015 abc1
2 1 2001 Product1 800 48700 11-2-2015 12-3-2015 xyz1
3 2 2002 Product2 1500 58500 5-3-2015 6-4-2015 qwe1
Payment
id(PK) wid(FK) clientid product rcvamt restamt
1 2 2001 Product1 1300 48700
2 3 2002 Product2 1500 58500
I want to show a report like
clientid company procudt total rcvamt restamt rcvdate nxtdate note
2001 Company1 Product1 50000 1300 48700 11-2-2015 12-3-2015 xyz1
2002 Company2 Product2 60000 1500 58500 5-3-2015 6-4-2015 qwe1
2003 Company3 Product3 70000 - - - - -
I tried to make it simple:
SELECT DISTINCT
C.clientid
, C.company
, C.product
, C.total
, P.rcvamt
, P.restamt
, ( SELECT TOP 1 rcvdate FROM PaymentData AS PD1 WHERE PD1.ClientID=PD.ClientID AND PD1.Product=PD.Product ORDER BY rcvdate DESC)
, ( SELECT TOP 1 nxtdate FROM PaymentData AS PD1 WHERE PD1.ClientID=PD.ClientID AND PD1.Product=PD.Product ORDER BY rcvdate DESC)
, ( SELECT TOP 1 Note FROM PaymentData AS PD1 WHERE PD1.ClientID=PD.ClientID AND PD1.Product=PD.Product ORDER BY rcvdate DESC)
FROM
Collection C
LEFT OUTER JOIN Payment P
ON C.clientid = P.clientid
LEFT OUTER JOIN PaymentData PD
ON P.clientid = PD.clientid
But I don't know all the relationships between the tables.
The Answer to your Question could look like this
Select Collection.clientid
,Collection.company
,Collection.product
,Collection.total
,Payment.rcvamt
,Payment.restamt
,PaymentData.rcvdate
,PaymentData.nxtdate
,PaymentData.Note
From PaymentData
Inner Join (Select wcid
,Max(PaymentData.rcvdate) as rcvdate
,Max(PaymentData.nxtdate) as nxtdate
FROM PaymentData
GROUP BY wcid) AS SubSelect ON PaymentData.wcid = SubSelect.wcid
AND PaymentData.rcvdate = SubSelect.rcvdate
AND PaymentData.nxtdate = SubSelect.nxtdate
Inner Join Payment on PaymentData.wcid = Payment.id
RIGHT OUTER JOIN Collection ON PaymentData.clientid = Collection.clientid
Here the sqlfiddle to prove my Answer.
Something like this should work. It appears you want an aggregation on the restamt, both other fields are the last payment received. I also assume this is SQL Server due to your name. If it's a different db, please provide the
UPDATE: Changed to LEFT JOIN to handle client 3 without products, fixed typo in product. SQL Fiddle: http://sqlfiddle.com/#!3/8ad566/19/0
SELECT
c.clientid,
c.company,
c.product,
c.total,
SUM(pd.rcvamt) AS rcvamt,
LastPayment.restamt,
LastPayment.rcvdate,
LastPayment.nxtDate,
LastPayment.note
FROM Collection c
LEFT OUTER JOIN PaymentData pd
ON pd.wcid = c.id
LEFT OUTER JOIN (
SELECT
wcid,
restamt,
rcvdate,
nxtdate,
Note,
ROW_NUMBER() OVER (PARTITION BY wcid ORDER BY rcvdate DESC) AS RowNum
FROM PaymentData
) LastPayment
ON LastPayment.wcid = c.id
AND LastPayment.RowNum = 1 -- Get last payment info
GROUP BY
c.clientid,
c.company,
c.product,
c.total,
LastPayment.restamt,
LastPayment.rcvdate,
LastPayment.nxtDate,
LastPayment.note
ORDER BY
c.clientid