简体   繁体   中英

SQL - with only showing a value greater than 0

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:

  • Never use commas in the FROM clause.
  • Always use proper, explicit, standard , readable JOIN syntax.
  • Use table aliases.
  • Qualify all column names. I don't know where they come from, but I would guess most should be preceded by 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM