SQL在查询中使用两个聚合函数

时间:2018-04-24 00:25:06

标签: sql ms-access

我需要创建一个查询,从名为INV_NUM的表中输出INV_AMOUNTAVG_INVDIFFINVOICE。< / p>

到目前为止,我有这个:

SELECT INV_NUM, 
       INV_AMOUNT, 
       (SELECT AVG(INV_AMOUNT) FROM INVOICE) AS AVG_INV 
FROM INVOICE;

我是sql的新手,我正试图弄清楚如何制作

(SELECT SUM(INV_AUMOUNT - AVG(INV_AMOUNT) FROM INVOICE) AS DIFF 

的工作。我已经搜索了一个解决方案,但无法实现有效的方法。感谢您的任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

你似乎想要这样的东西:

SELECT i.INV_NUM, i.INV_AMOUNT, ai.avg_inv_amount,
       (i.INV_AMOUNT - ai.avg_inv_amount) as diff
FROM INVOICE as i,
     (SELECT AVG(INV_AMOUNT) as avg_inv_amount FROM INVOICE) as ai;