SQL v访问表选择别名

时间:2016-09-08 09:48:47

标签: sql ms-access

我试图将一些SSMS SQL转换为Access SQL,并且发现整个过程相当令人沮丧!我有一个SQL在SSMS中运行得很好,但无法在Access中使用它。 SQL相对简单。它只是根据第二个表中的项目计数更新表中的一个字段。

update Summary_Complaint_Table set period1_count = sql.mycount from 
(
 select t2.category,count(t2.category)as mycount 
 from complaints t2 
 where t2.date_received between #1/9/2015# and #23/12/2016# 
 group by category
 ) as sql 
 where Summary_Complaint_Table.category = sql.category

当我用sql.count和sql_category替换值时,内部选择与外部更新一样完美。 我得到的错误是

Syntax error (missing operator) in query expression 'sql.mycount from    
(select t2.category,count(t2.category)as mycount from complaints t2 
 where t2.date_received between #1/9/2015# and #23/12/2016# 
 group by category) as sql'

有效的原始SSMS(SQL server 2005)语法是

update #temp set period1_count = sql.mycount
from 
(
select t2.category,count(t2.category)as mycount
from complaints t2
where t2.date_received between @period1_from and @period1_to
group by category
) as sql
where 
#temp.category = sql.category 

2 个答案:

答案 0 :(得分:2)

如果SQL中的任何部分中的函数包含聚合/组,则Access无法更新一个SQL中的数据。作为解决方法,您可以使用DCount函数而不是Count().. Group By。

答案 1 :(得分:0)

我相信你需要一个空间和一个“as”:

'sql.mycount from    
(select t2.category, count(*) as mycount from complaints as t2 
 where t2.date_received between #2015/09/01# and #2016/12/23# 
 group by category) as sql'

此外,当dd为12或更小时,dd / mm / yyyy日期序列将无效。