从多访问表中选择

时间:2017-11-14 10:37:52

标签: sql vb.net ms-access

尝试获取此查询的结果:

SELECT DISTINCT
       invoices.invoice_id,
       invoices.customer,
       invoices.total,
       SUM(pay_amount) AS paid
FROM invoices,
     invoice_payment
WHERE invoices.invoice_id = invoice_payment.invoice_id
      AND invoices.type = 'client'
      AND invoice_payment.pay_type <> 'postpaid'
      AND format(invoices.create_date, "dd/mm/yyyy") BETWEEN '01/11/2017' AND '14/11/2017';

我得到了

  

“您的查询不包含特定表达式'invoice_id'   集合函数的一部分“

1 个答案:

答案 0 :(得分:1)

在汇总数据时添加GROUP BY,例如数,总和等

像这样:

SELECT DISTINCT
       invoices.invoice_id,
       invoices.customer,
       invoices.total,
       SUM(pay_amount) AS paid
FROM invoices,
     invoice_payment
WHERE invoices.invoice_id = invoice_payment.invoice_id
      AND invoices.type = 'client'
      AND invoice_payment.pay_type <> 'postpaid'
      AND format(invoices.create_date, "dd/mm/yyyy") BETWEEN '01/11/2017' AND '14/11/2017'
GROUP BY invoices.invoice_id,
         invoices.customer,
         invoices.total;

GROUP BY的访问权限文档为here

同时尽量避免旧式连接,它们是讨厌和过时的。这会好得多:

FROM invoices
INNER JOIN invoice_payment ON invoices.invoice_id = invoice_payment.invoice_id