MS-Access SQL语法错误中的子查询

时间:2013-11-10 02:21:15

标签: sql ms-access

晚安。

我正在尝试创建一个平均每个产品购买量的查询。购买平均金额的购买次数。

这包括三个表:

  1. CUSTOMER
  2. 发票
  3. LINE
  4. 我写了这个:

    SELECT CUSTOMER.CUS_CODE, CUS_FNAME+" "+CUS_LNAME AS Name, CUSTOMER.CUS_BALANCE,    
    Sum([LINE]![LINE_NUMBER]*[LINE]![LINE_PRICE]) AS Total_purchases,  
    Count(INVOICE.INV_NUMBER) AS Number_of_purchases, 
    
    (
    SELECT [Sum([LINE]![LINE_NUMBER]*[LINE]![LINE_PRICE]) /   
    [Count(INVOICE.INV_NUMBER)]
    FROM INVOICE, LINE
    WHERE INVOICE.INV_NUMBER = LINE.INV_NUMBER
    ) AS Average_purchase_amount
    
    FROM CUSTOMER, INVOICE, LINE
    WHERE CUSTOMER.CUS_CODE = INVOICE.CUS_CODE AND INVOICE.INV_NUMBER = LINE.INV_NUMBER 
    GROUP BY CUSTOMER.CUS_CODE, CUS_FNAME+" "+CUS_LNAME, CUSTOMER.CUS_BALANCE;
    

    我在第二个SELECT时遇到错误。我不知道为什么。我觉得我错过了很简单的事情。

    实际错误为:

    • “查询表达式中的语法错误”(  SELECT [Sum([LINE]![LINE_NUMBER] * [LINE]![LINE_PRICE])/ [Count(INVOICE.INV_NUMBER)]。'“

    从错误中我认为在我的表达中的某些地方没有排队,但我想我不知道究竟发生了什么事情。

1 个答案:

答案 0 :(得分:1)

有不平衡的括号。要么每个都在括号或整个等式中:

SELECT [Sum([LINE]![LINE_NUMBER]*[LINE]![LINE_PRICE])] / [Count(INVOICE.INV_NUMBER)]

- 或 -

SELECT [Sum([LINE]![LINE_NUMBER]*[LINE]![LINE_PRICE]) /  Count(INVOICE.INV_NUMBER)]

尝试加入你的表格。像这样:

FROM INVOICE
    INNER JOIN CUSTOMER ON CUSTOMER.CUS_CODE = INVOICE.CUS_CODE
    INNER JOIN LINE ON LINE.INV_NUMBER =INVOICE.INV_NUMBER