I am wrote this SQL:
select distinct PayID, PaymentDate, AccountNumber, sum(PaymentAmount) as Amount
from tblInvoicePaymentDetails
where CustomerID = 'mud4978'
group by PayID, PaymentDate, AccountNumber
Output is:
PayID PayDate Account Amount
1 2011-07-05 11:09:14.390 NULL 700.00
1 2011-07-05 11:09:14.407 NULL 100.00
2 2011-07-05 11:20:05.517 NULL 0.00
2 2011-07-05 11:20:05.547 9000 500.00
2 2011-07-05 11:20:07.000 9000 100.00
3 2011-07-05 12:19:22.017 100000 200.00
3 2011-07-05 12:19:22.077 100000 100.00
But my requirements are to display 3 records (avoid duplicates) and sum of amount like this:
payid paydate account amount
1 2011-07-05 11:09:14 null 800.00
2 2011-07-05 11:20:05 9000 600.00
3 2011-07-05 12:19:22 100000 300.00
please help
It looks like you only want to group by PayID (otherwise other columns will be taken into account).
PayDate varies, so if you want one of those values you need to pick which exact PayDate you want in your output. You may wish to consider Max or Min to pick one that exists already.
Account also varies, so again you need to pick one using something like Min or Max
Your query would then look something like this: