I have a query in SQL in which I am presenting whether or not a person has payments or outstanding charges. In this query I have a case statement, but I need to group the records together to make sure payments are added together so that what I am checking for is correct.
Let me show the code and hopefully this will make a little more sense.
Here is the query I am currently using:
Declare @tmpTable Table
(
[Bid #] int,
Name varchar(200),
Spent numeric(18,2),
Paid numeric(18,2),
[Credit Card On File] varchar(3),
SaleCounter int,
Notes varchar(max)
)
Insert into @tmpTable([Bid #], Name, Spent, Paid, [Credit Card On File], SaleCounter, Notes)
Select s.[BidderNumber] as 'Bid #', ltrim(rtrim(b.bidderName)) as 'Name', isnull(s.saleprice * s.Quantity,0) as 'Spent',
isnull(t.Amount,0) as 'Paid',
case
when b.cconfile = 1 then 'Yes'
else
'No'
end as 'Credit Card On File',
s.SaleCounter, isnull(t.Notes, '') as 'Notes'
from sales s inner join Bidders b on s.BidderNumber = b.BidderNumber
Left outer join transactions t on t.BidderNumber = s.BidderNumber
order by s.Biddernumber, b.biddername, b.cconfile, SaleCounter
Select [Bid #], Name, Spent as 'Total Purchases', Paid as 'Current Payments',
case
when [Credit Card On File] = 'Yes' then
case
when cast(Paid as numeric(18,2)) = 0 then cast(Spent as numeric(18,2))
else
case when (sum(cast(Paid as numeric(18,2)))) > sum(cast(Spent as numeric(18,2))) then (cast(Paid as numeric(18,2)))- sum(cast(Spent as numeric(18,2)))
else (cast(Spent as numeric(18,2)) - cast(Paid as numeric(18,2)))
end
end
else 0
end as 'Amount To Charge Credit Card',
case
when [Credit Card On File] = 'No' then
case
when cast(Paid as numeric(18,2)) = 0 then cast(Spent as numeric(18,2))
else
case when (sum(cast(Paid as numeric(18,2)))) > sum(cast(Spent as numeric(18,2))) then (cast(Paid as numeric(18,2)))- sum(cast(Spent as numeric(18,2)))
else (cast(Spent as numeric(18,2)) - cast(Paid as numeric(18,2)))
end
end--sum(Outstanding)
ELSE 0
end as 'Outstanding Balance', Notes
from @tmpTable
group by [Bid #], name, spent, paid, [Credit Card On File], SaleCounter, Notes
order by [Bid #], Name, spent, paid, [Credit Card On File], SaleCounter, Notes
Here is the recordset that is returned and inserted into the @tmpTable:
Bid # Name Total Purchases Current Payments Amount To Charge Credit Card Outstanding Balance Notes
101 Tom & Joan Bergland 7500.00 0.00 0.00 7500.
102 John & Bonnie Black 50.00 50.00 0.00 0.00
108 Cindy Davidson 3600.00 1600.00 0.00 2000.00 250
108 Cindy Davidson 3600.00 2000.00 0.00 1600.00
109 Cynthia Davis 315.00 315.00 0.00 0.00 2355
117 Susan Harris 75.00 75.00 0.00 0.00
119 Jim & Julie Hill 520.00 0.00 520.00 0.00
125 Bill & Amy Lee 526.00 526.00 0.00 0.00
Now my issue is that I am showing a balance owed by Bid # 108 when there is no balance owed. The two records reflect two payments made against the balance and when the two are totaled together they equal the balance. I need to show each payment to the user, but when the payments equal the total purchase, then no balance owed needs to be shown. however, if there is a balance, that needs to be shown. (Credit Card Payments are different...what I am having issues with are non CC payments)
Is there a way to group inside the case statement or is there something in sql I am missing that will allow what I need to happen?
I appreciate your help in advance.