按日期访问SQL筛选器查询两个值

时间:2016-02-16 14:40:11

标签: sql ms-access

我有一个表达式,显示所有订单进出前的总库存:

(Query3只是将股票清单与进入和离开的订单组合在一起,并且工作正常)

SELECT Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand,
  Nz(Sum([OrderJoin.Quantity]),0) AS Outgoing, Nz(Sum([Query1.Quantity]),0) AS Incoming,
  [OnHand]+[Outgoing]-[Incoming] AS OnHandAfter, [StandardCost]*[OnHandAfter] AS TotalCost
FROM Query3
GROUP BY Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand;

但我一直试图通过用户输入的日期过滤它。我的代码不正确,我不知道为什么

SELECT Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand, 
  (SELECT NZ(Sum([OrderJoin.Quantity]),0)
  FROM Query3
  WHERE (((Query3.ShippedDate)>[Enter End Date] Or (Query3.ShippedDate) Is Null))
  GROUP BY Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand;
    ) AS Outgoing, 
  (SELECT NZ(Sum([Query1.Quantity]),0)
  FROM Query3
  WHERE (((Query3.DateReceived)>[Enter End Date] Or (Query3.DateReceived) Is Null))
  GROUP BY Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand;
    ) AS Incoming,
[OnHand]+[Outgoing]-[Incoming] AS OnHandAfter, [StandardCost]*[OnHandAfter] AS TotalCost
FROM Query3
GROUP BY Query3.Products.ID, Query3.ProductName, Query3.StandardCost, Query3.OnHand;

Access出现错误:"At most one record can be returned by this subquery"。有谁知道我做错了什么?

更新:

我发现在SELECT中使用嵌套的SELECT只会尝试使用一个值,但我想返回多个。所以我编写了一些代码,它出现了语法错误,我不确定原因:

SELECT Q1.Products.ID, Q1.ProductName, Q1.StandardCost, Q1.OnHand, 
  [Q1.OnHand]+[Q2.Outgoing]-[Q3.Incoming] AS OnHandAfter,
  [StandardCost]*[OnHandAfter] AS TotalCost
FROM (Query3 Q1
LEFT JOIN
  (SELECT Q2.Query3.Products.ID, Q2.Query3.ProductName, Q2.Query3.StandardCost,
    Q2.Query3.OnHand, NZ(Sum([OrderJoin.Quantity]),0) AS Outgoing
  FROM Query3 Q2
  WHERE (((Query3.ShippedDate)>[Enter End Date] Or (Query3.ShippedDate) Is Null))
  GROUP BY Q2.Query3.Products.ID, Q2.Query3.ProductName, Q2,Query3.StandardCost,
    Q2.Query3.OnHand) ON Q1.Query3.Products.ID = Q2.Query3.Products.ID) 
LEFT JOIN
  (SELECT Q3.Query3.Products.ID, Q3.Query3.ProductName, Q3.Query3.StandardCost,
    Q3.Query3.OnHand, NZ(Sum([Query1.Quantity]),0) AS Incoming
  FROM Query3 Q3
  WHERE (((Query3.DateReceived)>[Enter End Date] Or (Query3.DateReceived) Is Null))
  GROUP BY Q3,Query3.Products.ID, Q3.Query3.ProductName, Q3.Query3.StandardCost,
    Q3.Query3.OnHand) ON Q1.Query3.Products.ID = Q3.Query3.Products.ID
GROUP BY Q1.Query3.Products.ID, Q1.Query3.ProductName, Q1.Query3.StandardCost, Q1.Query3.OnHand;

1 个答案:

答案 0 :(得分:0)

这是来自类似问题的answer。使用std::deque<std::deque<int>> cont; is_element_container(cont); 函数,以便子查询只返回一行:

MAX