子选择联合的总和结果

时间:2014-04-16 11:52:06

标签: sql ms-access

嘿,我试图在合并然后在unio中将两个不同结果集的结果相加。目的是获得每种描述的计数差异。

使用下面的代码我从MS ACCESS的union语句中得到一个错误。 或者如果我在加入中获得错误后添加额外的parenthis。

任何想法????

select description, sum(CP)

from

(select  description, count(description)
*-1 AS CP
from status
where date_loaded between #1/1/2014# and #2/1/2014#
group by description) 
Union all
(select  description, count(description) AS CP
from status
where date_loaded between #1/1/2014# and #3/1/2014#
group by description) 

按说明分组

1 个答案:

答案 0 :(得分:2)

您不能在子查询中执行union(在MS Access的许多问题中)。幸运的是,您可以使用条件聚合执行所需操作:

select description,
       count(*) - sum(iif(date_loaded between #1/1/2014# and #2/1/2014#, 1, 0)) as diff
from status
where date_loaded between #1/1/2014# and #3/1/2014#
group by description;

我认为以下内容也符合您的要求:

select description,
       sum(iif(date_loaded between #2/2/2014# and #3/1/2014#, 1, 0)) as newest
from status
where date_loaded between #1/1/2014# and #3/1/2014#
group by description;