0

我在access 2010中有一个数据库。

A 有一个包含一些字段的表。其中三个是“输入”、“输出”和“日期”。A还有总量查询。

我想对所有具有相同“日期”的“输入”值求和,我还想对所有具有相同“日期”的“输出”值求和。

我希望我的查询找到扣除结果(“输入”-“输出”具有相同的“日期”)。

我的问题是如何关联具有相同“日期”值的不同插入。

请问有什么帮助吗?

编辑:

input     output      date
  3         0         2/5/2012
  4         0         8/5/2012
  0         2         2/5/2012
  0         1         8/5/2012

我希望我的查询显示

available stock     date
   (3-2=) 1       2/5/2012
   (4-1=) 3       8/5/2012

已编辑 v2:

我的sql代码是

 SELECT SUM(warehouse.in_quant)-SUM(warehouse.out_quant) AS SUM, 
 drugs.active_substance, drugs.strength, drugs.strength_type, drugs.dosage_form, 
 warehouse.available_stock, drugs.minimum_quantity, IIf([warehouse]![available_stock]
 [drugs]![minimum_quantity],"YES!","No") AS DiplayText, warehouse.curr_date
 FROM drugs INNER JOIN warehouse ON drugs.ID = warehouse.drug_id;

我收到这条消息:

您尝试执行的查询不包含指定表达式“active_substance”作为聚合函数的一部分。

当我删除 SUM(warehouse.in_quant)-SUM(warehouse.out_quant) AS SUM 时,它工作正常。

4

2 回答 2

1

Go to the query design window, add the relevant tables, drag the fields that you want to match from one table to the next. Test. If it is not what you want, post the sql back here with sample data and an explanation of the problem.

SELECT i.Input-o.Output As Stock, i.Date
FROM (SELECT Input FROM Table
      WHERE Input > 0) i
LEFT JOIN 
     (SELECT Output FROM Table
      WHERE Output > 0) o
ON i.Date = o.Date

You will probably need something more complicated, but this should be a start, hopefully.

Re comments

SELECT drugs.active_substance, 
       drugs.strength, 
       drugs.strength_type, 
       drugs.dosage_form, 
       warehouse.available_stock, 
       drugs.minimum_quantity, 
       warehouse.curr_date,
       SUM(warehouse.in_quant)-SUM(warehouse.out_quant) AS SUMQty
FROM drugs 
INNER JOIN warehouse ON drugs.ID = warehouse.drug_id
GROUP BY drugs.active_substance, 
       drugs.strength, 
       drugs.strength_type, 
       drugs.dosage_form, 
       warehouse.available_stock, 
       drugs.minimum_quantity, 
       warehouse.curr_date
于 2012-05-31T14:48:13.427 回答
1

I'm not quite sure what your asking but to sum values by a date you should use the group by clause

SELECT SUM(input) - SUM(output) as 'Input - Output', date 
FROM myTable 
GROUP BY date

Here is an example of it what I think your trying to do.

于 2012-05-31T14:48:49.260 回答