Login
`
Templates, Tools and Utilities
|
||
Add a comment to an Icetips ArticlePlease add your comments to this article.
Please note that you must provide both a name and a valid email address in order
for us to publish your comment. Comments are moderated and are not visible until they have been approved. Spam is never approved!
Back to article list Search Articles Add Comment Printer friendly Direct link SQL Related Articles: Sum Calculations in select statement and filtering based on value 2003-02-05 -- Andy Stapleton Newsgroups: TopSpeed.Products.Drivers
Let me take a stab at it...
SELECT
F.PayerID,
CAST(F.LastName + ', ' + F.FirstName AS VARCHAR(52)) AS 'Family
Name',
SUM(CASE WHEN L.TType = 'Charge' THEN L.Amount END) AS Charges,
SUM(CASE WHEN L.TType = 'Credit' THEN L.Amount END) AS Credits,
SUM(CASE WHEN L.TType = 'Payment' THEN L.Amount END) AS Payments,
SUM(CASE WHEN L.TType = 'Charge' THEN L.Amount WHEN L.TType =
'Credit' THEN 0 - L.Amount WHEN L.TType = 'Payment' THEN 0 - L.Amount END)
AS BalanceDue
FROM Ledger AS L, Family AS F
WHERE F.PayerID = L.PayerID
GROUP BY F.LastName, F.FirstName, F.PayerID
HAVING
SUM(CASE WHEN L.TType = 'Charge' THEN L.Amount
WHEN L.TType = 'Credit' THEN 0 - L.Amount WHEN L.TType = 'Payment' THEN 0 -
L.Amount
END) > 10.00
Have you tried the above?
--
Andy <
Today is November 21, 2024, 6:39 am This article has been viewed 35233 times. Google search has resulted in 3112 hits on this article since January 25, 2004.
|
|