1

I have 2 tables. I need to select the column name and a calculated field from Invoices called balance_due.

The result of the query should be the name and their balance due from all of their records combined.

Thanks for any help.

Mike Ebbert
  • 13
  • 1
  • 4

2 Answers2

1
SELECT v.vendor_name, i.totalbalance
FROM Vendors as v
     INNER JOIN (
           SELECT vendor_id, sum(invoice_total-payment_total) as totalbalance
           FROM invoices
           GROUP BY vendor_id
     ) as i on i.vendor_id = v.vendor_id
user1845791
  • 993
  • 2
  • 9
  • 17
  • Please edit your answer and elaborate, explaining how this will work. – Charlie Jun 08 '13 at 03:30
  • Thank you so much for the response. It is what I am looking for. However I get a 00933 error thrown at line 2 (command not properly ended). "FROM vendors as v" Any suggestions? – Mike Ebbert Jun 08 '13 at 03:39
0

Or there is another syntax:

;With i As
(
   SELECT vendor_id, sum(invoice_total-payment_total) as totalbalance
   FROM invoices
   WHERE payment_total is not null
   GROUP BY vendor_id
)
SELECT Vendors.vendor_name, i.totalbalance
From Vendors LEFT JOIN i ON Vendors.vendor_id = i.vendor_id
Khanh TO
  • 48,509
  • 13
  • 99
  • 115