简体   繁体   English

SQL - 只显示大于 0 的值

[英]SQL - with only showing a value greater than 0

I am fairly new to SQL.我对 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.我的问题是,我只需要显示总额超过 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:使用having子句:

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.切勿FROM子句中使用逗号。
  • Always use proper, explicit, standard , readable JOIN syntax.始终使用正确、明确、标准、可读的JOIN语法。
  • 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. .我不知道它们来自哪里,但我想大多数应该在I. .

he HAVING clause is often used with the GROUP BY clause to filter groups based on a specified list of conditions HAVING 子句通常与 GROUP BY 子句一起使用,以根据指定的条件列表过滤组

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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