请参阅上面的数据结构。我正在尝试编写SQL查询以获取每个会话的平均客户数
我的尝试:
select avg(A.NumberCustomer)
from(
select SessionName, count(distinct customers.Idcustomer) as NumberCustomer,
from customers, enrollments, sessions
where customers.Idcustomer=enrollments.Idcustomer and enrollments.Idsession=sessions.Idsession
group by sessions.SessionName
) A
但我似乎从客户,注册,会话行中得到错误 不确定这一点,任何帮助表示赞赏。
由于
答案 0 :(得分:3)
你有删除的额外逗号:
select avg(A.NumberCustomer)
from(
select SessionName,
count(distinct customers.Idcustomer) as NumberCustomer, #<--- here
from customers, enrollments, sessions
where customers.Idcustomer=enrollments.Idcustomer
and enrollments.Idsession=sessions.Idsession
group by sessions.SessionName
) A
顺便提一下,出于可读性原因,我建议您转到SQL'99连接语法:
SELECT
avg(A.NumberCustomer)
FROM (
select
SessionName,
count(distinct customers.Idcustomer) as NumberCustomer
from customers
inner join enrollments
on customers.Idcustomer=enrollments.Idcustomer
inner join sessions
on enrollments.Idsession=sessions.Idsession
group by sessions.SessionName
) A
此外,有问题的图表,请记住下次包含您的错误消息。
答案 1 :(得分:2)
对于每个会话中的平均客户数,您应该只能使用注册表。平均值是入学人数除以会话数:
select count(*) / count(distinct idSession)
from enrollments e;
这做出以下假设: