Combine and calculate invoices?

There's a table that retains:

  1. Invoices
  2. Payments
  3. Credits, etc ...

Example:

CustomerNo InvoiceNo TransactionType InvoiceType TransactionDate PaymentDate TransactionAmt
0000019 9322152 I IN 10/25/2017 216.81
0000019 9322152 P IN 10/25/2017 10/25/2017 216.81
0000048 8983992 C IN 1/5/2018 178.6
0000048 8983992 P IN 1/17/2018 1/17/2018 110
0000048 8996940 C IN 1/5/2018 47.5
0000048 8996940 P IN 1/17/2018 1/17/2018 92.5
0000048 9048367 C IN 1/5/2018 47.5
0000048 9048367 P IN 1/17/2018 1/17/2018 37.5
0000048 9521597 I IN 11/9/2017 54
0000048 9521597 P IN 1/17/2018 1/17/2018 54
0000048 9535989 I IN 11/10/2017 3588
0000048 9535989 P IN 1/17/2018 1/17/2018 3588
0000048 9541871 I IN 11/12/2017 62.5
0000048 9541871 P IN 1/17/2018 1/17/2018 62.5
0000048 9691811 I IN 11/21/2017 110
0000048 9691811 P IN 1/17/2018 1/17/2018 110
0000048 A049266 C IN 3/26/2018 267.5
0000048 A049266 I IN 12/7/2017 288.75
0000048 A049266 P IN 2/13/2018 2/13/2018 5.88
0000048 A049266 P IN 3/26/2018 3/26/2018 15.37

How can you result one row for each invoices with the calculated results based on the TransactionType?

Transaction Type Add/Subtract
A Adjustment Either way
B Balance Forward +
C Credit -
D Debit +
E Balance Forward Other Charges +
F Finance Charge +
I Invoice +
P Payment -
X PrePayment -
select invoiceno
      ,sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end) as balance
  from yourtable
 group by invoiceno

If you don't want to see zero balance, add this:

 having sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end)<>0

Hello again

So It's looking like it's not correct?

CustomerNo CustomerName InvoiceNo TransactionType InvoiceType TransactionDate PaymentReference CheckNo PaymentDate PaymentType OtherPaymentTypeRefNo TransactionAmt
0028874 DEMAND MEDIA A551989 I IN 1/8/2018 1/1/1753 17
0028874 DEMAND MEDIA A551989 P IN 3/2/2018 ACH 3/2/2018 17

This should show $0 or not at all if removing zero balances

It's showing this with the code provided: 17-17=0 and not -17
0028874 A551989 2018-03-02 00:00:00.000 -17.00

For every invoice there may or may not be a payment or credit, etc ... but every invoice should show and show based on if there was other transactions against but once with the calculated result.

Hope that helps?

How odd?
This is the code with the >= 1/1/2017

SELECT CustomerNo, InvoiceNo, TransactionDate, SUM(TransactionAmt * CASE WHEN transactiontype IN ('C', 'P', 'X') THEN - 1 ELSE 1 END) AS Balance FROM dbo.AR_TransactionPaymentHistory GROUP BY InvoiceNo, CustomerNo, TransactionDate HAVING (TransactionDate >= CONVERT(DATETIME, '2017-01-01 00:00:00', 102)) and sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end)<>0

image

And to add one more step, need to show TransactionDate of "I", the Invoice, 3/2/2018 was the payment date of the invoice. invoice date for this invoice was 1/8/2018

The reason you don't get the result you're looking for, is that you included transactiondate in the "group by".

techsupport18-2

Ah, however that is needed and at the invoice. This is to be used to determine Aging and the date of the invoice is required with the invoice balance.
The invoice balance is determined by the above as they all "live" in this one table.

How about this:

select customermo
      ,invoiceno
      ,min(case when transactiontype='I' then transactiondate else null end) as invoicedate
      ,sum(transactionamt*case when transactiontype in ('C', 'P', 'X') then - 1 else 1 end) as balance
  from dbo.at_transactionpaymenthistory
 group by invoiceno
         ,customerno
 having transactiondate>=cast('2017-01-01' as datetime)
    and sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end)<>0
;

Msg 8121, Level 16, State 1, Line 8
Column 'dbo.AR_TransactionPaymentHistory.TransactionDate' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.

Woops. Try this:

select customermo
      ,invoiceno
      ,min(case when transactiontype='I' then transactiondate else null end) as invoicedate
      ,sum(transactionamt*case when transactiontype in ('C', 'P', 'X') then - 1 else 1 end) as balance
  from dbo.at_transactionpaymenthistory
 where transactiondate>=cast('2017-01-01' as datetime)
 group by invoiceno
         ,customerno
 having sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end)<>0
;

Is there a way to not show, UNLESS it has an I, Invoice to the invoiceno?

CustomerNo CustomerName InvoiceNo TransactionType InvoiceType TransactionDate PaymentReference CheckNo PaymentDate
0028874 DEMAND MEDIA 7111023 P IN 1/5/2017 ACH 1/5/2017

interestingly the invoice we were discussing above is no longer showing?

select customerno ,invoiceno ,min(case when transactiontype='I' then transactiondate else null end) as invoicedate ,sum(transactionamt*case when transactiontype in ('C', 'P', 'X') then - 1 else 1 end) as balance from dbo.ar_transactionpaymenthistory where InvoiceNo = 'A551989' group by invoiceno ,customerno having sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end)<>0 ;

select customermo
      ,invoiceno
      ,min(case when transactiontype='I' then transactiondate else null end) as invoicedate
      ,sum(transactionamt*case when transactiontype in ('C', 'P', 'X') then - 1 else 1 end) as balance
  from dbo.at_transactionpaymenthistory
 where transactiondate>=cast('2017-01-01' as datetime)
 group by invoiceno
         ,customerno
 having sum(transactionamt*case when transactiontype in ('C','P','X') then -1 else 1 end)<>0
    and sum(case when transactiontype='I' then 1 else 0 end)>0
;
1 Like

ok, looking good, remove the having clause to check on the invoice since that will not show $0 balances.

Thanks again! you are the master!!

Hopefully last question here, can I still use the TransactionDate with the code to do further formulas?

For instance this works:
DATEDIFF(d, TransactionDate, '3/31/2018') As Aged
but is the TransactionDate the formulated "InvoiceDate" that you have?

These do not: The + 30 to the "invoicedate" is considered "invoiceduedate"

CASE WHEN DATEDIFF(d, TransactionDate, '3/31/2018')+30) < 31 THEN TransactionAmt ELSE 0 END As Current

CASE WHEN DATEDIFF(d, TransactionDate, '3/31/2018')+30) BETWEEN 31 AND 60 THEN TransactionAmt ELSE 0 END As 1-30 Days, etc ...

Never mind. I saved it as a View and connected to it in Excel and did all the formulas for aging in there. Thanks again!