连接表和过滤器并采取聚合函数sql访问

时间:2013-05-21 04:04:59

标签: mysql ms-access-2007

是否可以连接两个表,过滤数据并在MS访问中获取聚合函数?我有两张桌子

Inv_details:invnum,itmcde,qnt,price  
Inv_summary:invnum,date,total  

我需要加入这些表并使用日期范围过滤它们,并通过itmcde得到qnt和价格顺序的总和。

我试过这样的事情。

SELECT inv.itmcde,SUM(inv.qnt),SUM(inv.price) FROM (SELECT inv_s.invnum,inv_s.date,inv_d.itmcde,inv_d.total FROM Inv_summary as inv_s INNER JOIN Invoice_details as inv_d ON inv_d.invnum =inv_s.invNum WHERE inv_s.date BETWEEN  #3/4/2013# And #5/16/2013# ) as inv ORDER BY inv.itmcde

但这不起作用。

1 个答案:

答案 0 :(得分:0)

尝试

SELECT d.itmcde,
       SUM(d.qnt) AS qnt,
       SUM(d.price) AS price
  FROM Inv_summary s INNER JOIN Inv_details d 
    ON d.invnum = s.invnum 
 WHERE s.date BETWEEN #3/4/2013# AND #5/16/2013# 
 GROUP BY d.itmcde
 ORDER BY d.itmcde

SQLFiddle 虽然这个小提琴适用于SQL Server,但同样的一般规则应适用于MS Access