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?
 
GranModCommented:
Closed, 50 points refunded.
GranMod
The Experts Exchange
Community Support Moderator of all Ages
0
 
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
0
 
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
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
tagtekinAuthor Commented:
bascially it needs to be T-sql form
0
 
Anthony PerkinsCommented:
>>bascially it needs to be T-sql form <<
Can you describe what is a "T-sql form".
0
 
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
0
 
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
0
 
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?
0
 
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
0
 
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.
0
 
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
0
 
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
0
 
tagtekinAuthor Commented:
please close question
0
 
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>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.