SQL查询,用于报告尚未支付的发票,按发票编号排序

时间:2014-03-06 17:46:53

标签: sql oracle

SQL新手,只做了一个半星期。所以我现在为这个简单或看似愚蠢的问题道歉。

我想提供一份报告,说明在给定日期之前尚未支付的发票,按发票编号排序。

这是我显示的方式,付费发票但没有给定日期..我如何显示31-MAR-14未支付的发票。

SELECT INVOICE.INVOICE_NUMBER, INVOICE.INVOICE_DATE, PAYMENT.PAYMENT_NO, PAYMENT.INVOICE_NUMBER
FROM INVOICE, PAYMENT
WHERE INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
ORDER BY INVOICE.INVOICE_NUMBER;

2 个答案:

答案 0 :(得分:1)

    SELECT PAYMENT.PAYMENT_NO
    FROM INVOICE, PAYMENT
    WHERE INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
    AND INVOICE.INVOICE_DATE = 'AAAAMMJJ'
    ORDER BY INVOICE.INVOICE_NUMBER;

尝试这样的事情。 我有问题。怎么说发票没有支付?

答案 1 :(得分:0)

您需要将这两个表与LEFT JOIN一起加入,然后查看付款日期的位置> 31-MAR-14。尝试这样的事情:

SELECT INVOICE.INVOICE_NUMBER, INVOICE.INVOICE_DATE, PAYMENT.PAYMENT_NO, PAYMENT.INVOICE_NUMBER
FROM INVOICE
LEFT JOIN PAYMENT ON INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
WHERE PAYMENT.Date_of_payment > '3/31/2014'
ORDER BY INVOICE.INVOICE_NUMBER;

这将查找2014年3月31日之后所做的所有付款。您可能需要添加另一个条件,以限制您要查找的发票。

要检查尚未付款的付款,请查看PAYMENT表中任何字段为空的位置。像这样的SQL:

SELECT INVOICE.INVOICE_NUMBER, INVOICE.INVOICE_DATE, PAYMENT.PAYMENT_NO, PAYMENT.INVOICE_NUMBER
FROM INVOICE
LEFT JOIN PAYMENT ON INVOICE.INVOICE_NUMBER = PAYMENT.INVOICE_NUMBER
WHERE INVOICE.INVOICE_DATE = '3/31/2014'
AND PAYMENT.PAYMENT_NO IS NULL
ORDER BY INVOICE.INVOICE_NUMBER;

此情况下的LEFT JOIN将始终返回INVOICE表的值,但如果尚未输入付款,则可能会返回NULL值来代替PAYMENT。检查AND PAYMENT.PAYMENT_NO IS NULL会告诉您尚未付款。