SQL to return balances

im trying to write a script that declares and sets a variable that’s equal to the total outstanding balance due. If that balance due is greater than $10,000.00, the script should return a result set consisting of VendorName, InvoiceNumber, InvoiceDueDate, and Balance for each invoice with a balance due, sorted with the oldest due date first. If the total outstanding balance due is less than $10,000.00, return the message “Balance due is less than $10,000.00.”

i have this so far but  it does not return the individual balances by looking at the declare variable.
US Ap
Delcare @totbaldue money
set @totbaldue = (select sum(invoicetotal-paymenttotal-credittotal) from invoices)

select vendorname, invoicenumber, invoicedate, @totbaldue as balance
from vendors join invoices on vendors.vendoris=invoices.vendorid

tagtekinAsked:
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.

Anthony PerkinsCommented:
Something like this perhaps:

select      v.vendorname,
      i.invoicenumber,
      i.invoicedate,
      SUM(i.invoicetotal - i.paymenttotal - i.credittotal) balance,
      CASE
            WHEN SUM(i.invoicetotal - i.paymenttotal - i.credittotal) < 10000 THEN 'Balance due is less than $10,000.00.'
            ELSE Null
      END Message
from      vendors v
      join invoices i on v.vendoris = i.vendorid
Group By
      v.vendorname,
      i.invoicenumber,
      i.invoicedate
Order By
      i.invoicedate DESC
tagtekinAuthor Commented:
yes thats by looping thgroug the data however i have to do it by declaring and setting the variable at the begining.
thanks
tagtekinAuthor Commented:
bascially it needs to be T-sql form
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Anthony PerkinsCommented:
>>bascially it needs to be T-sql form <<
Can you describe what is a "T-sql form".
Arthur_WoodCommented:
when you say "i have to do it by declaring and setting the variable at the begining", this sounds very much like a homework assignment - why else would you be required to do this in any particualr fashion?

IF this is a homework assignment, then EE Experts are NOT permitted to provide you with a complete answer.  We can assist you in figuring out the correct answer for yourself, but please do not ask any of us to do the work for you.

AW
tagtekinAuthor Commented:
I already put my script in the question but im having problems where i m doing it worng I m not asking anybody to do my hw.

THanks regards
Orcun Tagtekin
Anthony PerkinsCommented:
That link told me nothing about the infamous "T-sql form", icould not find the phrase any where in that article.  I then did a search on Google and came up with zip.  So is there any chance you can give in your words what you mean?
Arthur_WoodCommented:
I think he is refereing to SQL Server T-SQL (Transact-SQL) - the SQL Server Stored Procedure programming language.

This is from SQL Server Books OnLine:

"Transact-SQL is central to using SQL Server. All applications that communicate with an instance of SQL Server do so by sending Transact-SQL statements to the server, regardless of the user interface of the application"

But that does not explain what tagtekin means by 'T-SQL form' and he has not answered my question about this being a Homework/class assignment.

AW
Anthony PerkinsCommented:
>>But that does not explain what tagtekin means by 'T-SQL form' <<
I managed to figure out the T-SQL = Transact-SQL part :)

But it is the phrase "T-SQL form" that is totally meaningless.
tagtekinAuthor Commented:
I guess  I finally fogured it out. Since I only need to loop through the records.

MY state would be correct with this and here is what i got.
US Ap
Delcare @totbaldue money
set @totbaldue = (select sum(invoicetotal-paymenttotal-credittotal) from invoices)
if
@totbaldue >10000
select vendorname, invoicenumber, invoicedate, @totbaldue as balance
from vendors join invoices on vendors.vendoris=invoices.vendorid
else
print 'There are outstanding balances'

and thats about it
Anthony PerkinsCommented:
Great!  Please close the question.  Here is how from the EE Help:

I answered my question myself. What do I do?
http://www.experts-exchange.com/Databases/help.jsp#hi70
tagtekinAuthor Commented:
please close question
Anthony PerkinsCommented:
This is how it works here, form the link I posted previously:

<quote>
I answered my question myself. What do I do?
 
Post a question in the Community Support topic area asking for a refund, and asking the Moderators to close the question. You'll be required to post your solution in your original question. A Moderator will post a notice of your request which will give the participants 96 hours to object to the refund. Note that if it resembles one of the suggested comments, the likelihood is that your request will not be granted, but rather, the points will be awarded to the Expert who makes the suggestion. In your Community Support request, remember to post a link to the original question.
</quote>
GranModCommented:
Closed, 50 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages

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
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
Databases

From novice to tech pro — start learning today.