SQL Query Past Due Query

I am trying to create a aging report using SQL.

I'm trying to grab the balance for all customers who had a invoice due in the previous month. So if it's March, then I want to show all the balances for invoices that were due in February.

This is what I came up with, but I keep getting 0 for the Customers that I know have balances in the month of February.



SELECT arar_cust_n, arar_due_date,
case
when arar_due_date = Month(DATEADD(mm, -1, GETDATE()))
then arar_balance
else 0
End as thirtydaypassed
from arar


Please help.
LVL 1
isamesAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott PletcherSenior DBACommented:
SELECT arar_cust_n, arar_due_date,
 case
 when arar_due_date >= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0) AND
    arar_due_date < DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
then arar_balance
 else 0
 End as thirtydaypassed
 from arar

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Saurabh Singh TeotiaCommented:
You can simply use this as well..

select arar_cust_n, arar_due_date,arar_balance
from arar
where month(arar_due_date) = month(getdate())-1 and year(arar_due_date)=year(getdate())

Open in new window


Saurabh...
isamesAuthor Commented:
Your query worked.

Would you explain it to me?
Scott PletcherSenior DBACommented:
Sure.  This part:
DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)
is a very efficient and common technique for getting the first day, at midnight, of the current month, for example, if run today it returns:
2015-03-01 00:00:00.000

Now, since you want the previous month only, I subtract 1 month from that to get the starting day of the previous month to be the lowest date:
>= DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()) - 1, 0)

To make sure we don't count anything past last month, we use < (less than, not <=) of the first day of the current month:
< DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0)

This technique means the code keeps working correctly even if the data type of the column being compared changes.  This code works accurately for any date/datetime data type, while the <= methods do NOT.

Similarly, if I wanted to get all data for 2014, I should code it this way:
column >= '20140101' AND column < '20150101'

Or, more generically:
column >= DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()) - 1, 0) AND
column < DATEADD(YEAR, DATEDIFF(YEAR, 0, GETDATE()), 0)

This approach is very flexible and very efficient.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.