0

这是我正在尝试的查询。

select
    UserId,
    count(e.UserId) as 'Experiments',
    count(t.TaskId) as 'Tasks tried',
    count(case when t.TaskStatus = 'Completed' then t.TaskStatus end) as 'Tasks completed',
    sec_to_time(avg(timediff(TaskLocalUserEndDateTime,TaskLocalUserStartDateTime))) as 'Average Time'
from
    Tasks as t,
    Experiments as e
where
    t.ExperimentId = e.ExperimentId
and
    e.UserId = e.UserId
group by
    UserId;

count(e.UserId)count(t.TaskId)给出与哪个错误相同的结果。但是count(),如果我在没有任何连接的情况下运行查询,则会返回正确的结果,例如:

select
    count(UserId)
from
    Experiments
where
    UserId = UserId
group by 
    UserId;
4

1 回答 1

0

不确定这是否会有所帮助,但如果您在查询下方运行,您是否会获得所需的结果 - 请参阅 group by:

select
    count(UserId), ExperimentId
from
    Experiments
where
    UserId = UserId
group by 
    ExperimentId;

只需检查查询 - 注意到 Tasks 和 Experiments 表之间的连接位于 ExperimentId 上 - 在您声明的查询中,您仅使用 userId 获得所需结果。让我知道这是否有帮助。

select
    UserId,
    e.Experiments, -- count(e.UserId) as 'Experiments',
    count(t.TaskId) as 'Tasks tried',
    count(case when t.TaskStatus = 'Completed' then t.TaskStatus end) as 'Tasks completed',
    sec_to_time(avg(timediff(TaskLocalUserEndDateTime,TaskLocalUserStartDateTime))) as 'Average Time'
from
    Tasks as t,
--    Experiments as e 
LEFT JOIN 
(
   select count(UserId) as 'Experiments', ExperimentId from Experiments group by ExperimentId
) as e
where
    t.ExperimentId = e.ExperimentId
and
    e.UserId = e.UserId
group by
    UserId;
于 2013-02-09T06:12:26.743 回答