I am fairly new to SQL. I have my code written out where I display a Vendor name and the total amt due for all of their invoices. My issue is, I only need to display those Vendors that has a total greater than $0.00. Any insight would be greatly appreciated.
SELECT DISTINCT VENDOR_NAME, TO_CHAR(SUM(INVOICE_TOTAL-PAYMENT_TOTAL-CREDIT_TOTAL), '$999,999.99') AS AMTDUE
FROM AP.VENDORS, AP.INVOICES
WHERE AP.VENDORS.VENDOR_ID = AP.INVOICES.VENDOR_ID
GROUP BY VENDOR_NAME;
Code displays:
Use a having
clause:
SELECT VENDOR_NAME,
TO_CHAR(SUM(INVOICE_TOTAL-PAYMENT_TOTAL-CREDIT_TOTAL), '$999,999.99') AS AMTDUE
FROM AP.VENDORS V JOIN
AP.INVOICES I
ON V.VENDOR_ID = I.VENDOR_ID
GROUP BY VENDOR_NAME
HAVING SUM(INVOICE_TOTAL - PAYMENT_TOTAL-CREDIT_TOTAL) > 0;
Notes:
FROM
clause.JOIN
syntax.I.
.he HAVING clause is often used with the GROUP BY clause to filter groups based on a specified list of conditions
SELECT DISTINCT VENDOR_NAME, TO_CHAR(SUM(INVOICE_TOTAL-PAYMENT_TOTAL-CREDIT_TOTAL), '$999,999.99') AS AMTDUE
FROM AP.VENDORS, AP.INVOICES
WHERE AP.VENDORS.VENDOR_ID = AP.INVOICES.VENDOR_ID
GROUP BY VENDOR_NAME
HAVING SUM(INVOICE_TOTAL - PAYMENT_TOTAL-CREDIT_TOTAL) > 0;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.