用这个语法错误拉我的头发

时间:2012-06-01 04:22:42

标签: sql ms-access count

我似乎遇到了查询语法错误但似乎无法隔离它。我正在使用MS Access,当我运行查询时,我在FROM子句中遇到语法错误。

我有两张桌子,他们是一对多的关系:

表1称为(客户),包含以下字段:

ID
FirstName

表2使用以下字段调用(tblservice):

serviceID
Timing
Total
customerID   <-Foreign Key

第一次查询:

select c.id, c.firstname, avg(s.Total) / (select count(id) from customer) as LifetimeValue
from tblservice as s join customer as c on s.id = c.id
group by s.id

第二次查询(30天跨度):

select c.id, c.firstname, avg(s.Total) / (select count(id) from customer) as LifetimeValue
from tblservice as s join customer as c on s.id = c.id
where (s.Timing)>=DateAdd("d",-30,Date())
group by s.id

2 个答案:

答案 0 :(得分:1)

试试这个:

select c.id, c.firstname, avg(s.Total) / count(c.id) as LifetimeValue
from tblservice as s inner join customer as c on s.id = c.id
group by c.id, c.firstname

select c.id, c.firstname, avg(s.Total) / count(c.id) as LifetimeValue
from tblservice as s inner join customer as c on s.id = c.id
where (s.Timing)>=DateAdd("d",-30,Date())
group by c.id, c.firstname

除非您按他们分组,否则无法选择c.idc.firstname。您可以使用count(c.id),因为您已经按c.id进行分组。我没有在MS Access中使用SQL。所以我不是百分百肯定。尝试一下。

答案 1 :(得分:1)

MS Access可能要求您使用INNER JOIN而非JOIN