Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Query Improvement

Author  Topic 

slihp
Yak Posting Veteran

61 Posts

Posted - 2007-11-14 : 06:02:42
In our business we receive debt from clients that we work in order to try and reclaim some of the debt on their behalf. When a debt file comes in from a client it is loaded onto the system and every entry in that debt file is stamped with the same batch id. Using the batch id we track each debt file and monitor how well we are working that debt file. So as an example we could receive a debt file with 100 records in it each of these records would be stamped with a batch id of say 100001, the next file that is loaded onto the system, each record would then be stamped with 100002. so we can track how each batch is doing by grouping by batch id.

I have written my query that basically groups all the accounts on the system by batch id. This is fine when I only want to return totals and sums in the select list. The way I have the written the query If I want to calculate the payments we have received for each batch or the commission we will make on the payments (or any other info per batch) I have to use subquerys in the select list using the batchid I have grouped by . Is this the best/only way to achieve what I want to do.


Any input most welcome.



sql below:




Select c.Name,ib.ImportBatchID BatchID,
---------------------------------------------------------------
CONVERT(VARCHAR(10),ib.UpdatedOn,103) LoadedOn,
---------------------------------------------------------------
Count(d.DebtID) Reg_Count, --No. Reg
---------------------------------------------------------------
Sum(d.DebtValue) Reg_Value, --Value Reg
---------------------------------------------------------------


(Select sum(da.Amount) from dbo.DebtAdjustment da WITH (NOLOCK), dbo.ImportBatchItem bi WITH (NOLOCK) where
bi.ItemID=da.DebtID And bi.ImportBatchID=ib.ImportBatchID) Adjustments,
---------------------------------------------------------------
(Select count(dh.DebtID) from dbo.Debthistory dh WITH (NOLOCK), dbo.ImportBatchItem bi WITH (NOLOCK) where --No. OBC's
bi.ItemID=dh.DebtID And dh.Note like 'OBC:%' And bi.ImportBatchID=ib.ImportBatchID) OBC_Num,
---------------------------------------------------------------
(Select count(dh.DebtID) from dbo.Debthistory dh WITH (NOLOCK), dbo.ImportBatchItem bi WITH (NOLOCK) where --No. ICC's
bi.ItemID=dh.DebtID And dh.Note like 'ICC:%' And bi.ImportBatchID=ib.ImportBatchID) ICC_Num,
---------------------------------------------------------------
(Select count(dh.DebtID) from dbo.ImportBatchItem bi WITH (NOLOCK), dbo.DebtHistory dh WITH (NOLOCK)
Where dh.DebtID=bi.ItemID AND bi.ImportBatchID=ib.ImportBatchID AND dh.UserName='Letter Server' AND dh.Note like '%Letter%') ItemsMailed,
---------------------------------------------------------------
Cast((Select sum(CASE
WHEN dp.ReceivedByID = 1
THEN dp.Amount * (tF.Rate /100)
WHEN dp.ReceivedByID = 2
THEN dp.Amount * (tD.Rate /100)
ELSE
dp.Amount * ((tF.Rate + tFe.Rate) / 100)
END)
From
dbo.DebtPayment dp JOIN dbo.Debt d ON d.DebtID=dp.DebtID
JOIN dbo.ImportBatchItem bi ON dp.DebtID=bi.ItemID

LEFT JOIN dbo.mTrackerFeeChange tF ON tF.ClientID=d.ClientID
AND tF.ContractID=d.ContractID AND dp.ReceivedByID=tF.RateType
AND (
(dp.PaymentOn >= tF.StartDate AND dp.PaymentOn <= tF.EndDate)
OR
(dp.PaymentOn >= tF.StartDate AND tF.EndDate IS NULL)
)

LEFT JOIN dbo.mTrackerDirectChange tD ON tD.ClientID=d.ClientID
AND tD.ContractID=d.ContractID AND dp.ReceivedByID=tD.RateType
AND (
(dp.PaymentOn >= tD.StartDate AND dp.PaymentOn <= tD.EndDate)
OR
(dp.PaymentOn >= tD.StartDate AND tD.EndDate IS NULL)
)

LEFT JOIN dbo.mTrackerFieldChange tFe ON tFe.ClientID=d.ClientID
AND tFe.ContractID=d.ContractID AND tFe.RateType=dp.ReceivedByID
AND (
(dp.PaymentOn >= tFe.StartDate AND dp.PaymentOn <= tFe.EndDate)
OR
(dp.PaymentOn >= tFe.StartDate AND tFe.EndDate IS NULL)
)
where bi.ImportBatchID=ib.ImportBatchID) AS decimal(10,2)) ComRate,

---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 1 AND Debt.OutstandingValue > 0) Girobank,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 2 AND Debt.OutstandingValue > 0) StandingOrder,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 3 AND Debt.OutstandingValue > 0) CreditCard,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 4 AND Debt.OutstandingValue > 0) DirectDebit,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 5 AND Debt.OutstandingValue > 0) Cheque,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 6 AND Debt.OutstandingValue > 0) PostalOrder,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 7 AND Debt.OutstandingValue > 0) Cash,
---------------------------------------------------------------
(SELECT sum(Debt.OutstandingValue) TotalValue From Debt WITH (NOLOCK), DebtStatus WITH (NOLOCK), ImportBatchItem bi WITH (NOLOCK)
WHERE Debt.Status=DebtStatus.DebtStatusID AND bi.ItemID=Debt.DebtID And bi.ImportBatchID=ib.ImportBatchID AND
DebtStatus.Description LIKE ('%' + 'Arrangement' + '%') AND Debt.AgreementPaymentMethodID = 8 AND Debt.OutstandingValue > 0) BankersDraft
---------------------------------------------------------------



From
dbo.Client c WITH (NOLOCK), dbo.Debt d WITH (NOLOCK), dbo.ImportBatchItem ib WITH (NOLOCK)
Where
c.ClientID=d.ClientID
AND
d.DebtID=ib.ItemID
AND
(@ClientID IS NULL OR c.ClientID = @ClientID)
Group by c.Name, ib.ImportBatchID,CONVERT(VARCHAR(10),ib.UpdatedOn,103),DATENAME(Month, ib.UpdatedOn) + ' ' + DATENAME(Year, ib.UpdatedOn)
Order by ib.ImportBatchID

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-11-14 : 07:09:52
Something similar to this?
SELECT		c.Name,
ib.ImportBatchID AS BatchID,
CONVERT(VARCHAR(10), ib.UpdatedOn, 103) AS LoadedOn,
COUNT(d.DebtID) AS Reg_Count,
SUM(d.DebtValue) AS Reg_Value,
SUM(COALESCE(Peso.Adjustments, 0)) AS Adjustments,
SUM(COALESCE(Yeti.OBC_Num, 0)) AS OBC_Num,
SUM(COALESCE(Yeti.ICC_Num, 0)) AS ICC_Num,
SUM(COALESCE(Yeti.ItemsMailed, 0)) AS ItemsMailed,
SUM(COALESCE(Yak.ComRate,
SUM(COALESCE(h.Girobank, 0)) AS Girobank,
SUM(COALESCE(h.StandingOrder, 0)) AS StandingOrder,
SUM(COALESCE(h.CreditCard, 0)) AS CreditCard,
SUM(COALESCE(h.DirectDebit, 0)) AS DirectDebit,
SUM(COALESCE(h.Cheque, 0)) AS Cheque,
SUM(COALESCE(h.PostalOrder, 0)) AS PostalOrder,
SUM(COALESCE(h.Cash, 0)) AS Cash,
SUM(COALESCE(h.BankersDraft, 0)) AS BankersDraft
FROM dbo.Client AS c WITH (NOLOCK)
INNER JOIN dbo.Debt AS d WITH (NOLOCK) ON d.ClientID = c.ClientID
INNER JOIN dbo.ImportBatchItem AS ib WITH (NOLOCK) ON ib.ItemID = d.DebtID
LEFT JOIN (
SELECT bi.ImportBatchID,
SUM(CONVERT(DECIMAL(10,2), CASE
WHEN dp.ReceivedByID = 1 THEN dp.Amount * (tF.Rate /100)
WHEN dp.ReceivedByID = 2 THEN dp.Amount * (tD.Rate /100)
ELSE dp.Amount * ((tF.Rate + tFe.Rate) / 100)
END) AS ComRate
FROM dbo.DebtPayment AS dp
INNER JOIN dbo.Debt AS d ON d.DebtID = dp.DebtID
INNER JOIN dbo.ImportBatchItem AS bi ON bi.ItemID = dp.DebtID
LEFT JOIN dbo.mTrackerFeeChange tF ON tF.ClientID = d.ClientID
AND tF.ContractID = d.ContractID
AND tF.RateType = dp.ReceivedByID
AND dp.PaymentOn >= tF.StartDate
AND dp.PaymentOn <= COALESCE(tF.EndDate, dp.PaymentOn)
LEFT JOIN dbo.mTrackerDirectChange AS tD ON tD.ClientID = d.ClientID
AND tD.ContractID = d.ContractID
AND tD.RateType = dp.ReceivedByID
AND dp.PaymentOn >= tD.StartDate
AND dp.PaymentOn <= COALESCE(tD.EndDate, dp.PaymentOn)
LEFT JOIN dbo.mTrackerFieldChange AS tFe ON tFe.ClientID = d.ClientID
AND tFe.ContractID = d.ContractID
AND tFe.RateType = dp.ReceivedByID
AND dp.PaymentOn >= tFe.StartDate
AND dp.PaymentOn <= COALESCE(tFe.EndDate, dp.PaymentOn)
GROUP BY bi.ImportBatchID
) AS Yak ON Yak.ImportBatchID = ib.ImportBatchID
LEFT JOIN (
SELECT DebtID,
SUM(CASE WHEN Note LIKE 'OBC:%' THEN 1 ELSE 0 END) AS OBC_Num,
SUM(CASE WHEN Note LIKE 'ICC:%' THEN 1 ELSE 0 END) AS ICC_Num,
SUM(CASE WHEN Note LIKE '%Letter%' AND UserName = 'Letter Server' THEN 1 ELSE 0 END) AS ItemsMailed
FROM dbo.DebtHistory WITH (NOLOCK)
GROUP BY DebtID
) AS Yeti ON Yeti.DebtID = d.DebtID
LEFT JOIN (
SELECT DebtID,
SUM(Amount) AS Adjustments
FROM dbo.DebtAdjustment WITH (NOLOCK)
GROUP BY DebtID
) AS Peso ON Peso.DebtID = d.DebtID
LEFT JOIN (
SELECT Debt.DebtID,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 1 THEN Debt.OutstandingValue ELSE 0 END) AS Girobank,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 2 THEN Debt.OutstandingValue ELSE 0 END) AS StandingOrder,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 3 THEN Debt.OutstandingValue ELSE 0 END) AS CreditCard,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 4 THEN Debt.OutstandingValue ELSE 0 END) AS DirectDebit,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 5 THEN Debt.OutstandingValue ELSE 0 END) AS Cheque,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 6 THEN Debt.OutstandingValue ELSE 0 END) AS PostalOrder,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 7 THEN Debt.OutstandingValue ELSE 0 END) AS Cash,
SUM(CASE WHEN Debt.AgreementPaymentMethodID = 8 THEN Debt.OutstandingValue ELSE 0 END) AS BankersDraft
FROM Debt WITH (NOLOCK)
INNER JOIN DebtStatus WITH (NOLOCK) ON DebtStatus.DebtStatusID = Debt.Status
WHERE DebtStatus.Description LIKE '%Arrangement%'
AND Debt.OutstandingValue > 0
GROUP BY Debt.DebtID
) AS h ON h.DebtID = d.DebtID
WHERE c.ClientID = @ClientID
OR @ClientID IS NULL
GROUP BY c.Name,
ib.ImportBatchID,
CONVERT(VARCHAR(10), ib.UpdatedOn,103),
DATENAME(MONTH, ib.UpdatedOn) + ' ' + DATENAME(Year, ib.UpdatedOn)
ORDER BY ib.ImportBatchID
iIf ti doesn't work, please do not ask again. We can't fix your query!
We have no access to your data so we can't unit test the suggestions.
What we can do, is to make pointer in which direction you should head on this.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

slihp
Yak Posting Veteran

61 Posts

Posted - 2007-11-14 : 08:02:05
thanks for the reply Peso. I will try your suggestion for comparison. My original query dose work, but i am just wanting to know if my solution is the best or is it better performance to use joins the way you have?
Go to Top of Page
   

- Advertisement -