我该如何编写查询来查找到期余额高于所有未付发票的平均余额?

时间:2019-04-02 21:06:57

标签: sql nested-query

编写一个查询,该查询返回具有InvoiceDate的发票  2016年4月1日之前,且余额应高于  所有未付发票的平均余额。的  查询结果应显示InvoiceNumber,InvoiceDate,  每个此类发票的发票总额和余额。分类  查询结果按Balancedue降序排列。 (注意:应付余额= InvoiceTotal – PaymentTotal -CreditTotal 未付发票是余额为<> 0)* /

的发票
        PaymentTotal 
        - CreditTotal AS BalanceDue
        FROM Invoices
        WHERE InvoiceDate < 2016-04-01 AND InvoiceTotal - PaymentTotal 
       - CreditTotal > 
                    (SELECT InvoiceTotal - PaymentTotal 
                    - CreditTotal AS BalanceDue
                    FROM Invoices
                    HAVING AVG(InvoiceTotal - PaymentTotal 
                    - CreditTotal) <> 0)

我已经尝试了上述方法,但似乎无法正确解决问题。

0 个答案:

没有答案