Mysql SQL-具有和分组的问题

Mysql SQL-具有和分组的问题,mysql,sql,grouping,aggregate,having,Mysql,Sql,Grouping,Aggregate,Having,嗨,我对一个曾经有效的查询有问题。我的SQL技能不是很好,不知道我缺少了什么。或者如果这是正确的方法。也许用临时表代替 基本要点是给定一个特定的时间框架,我需要计算5个班级的最高积分 测试分数-保留分数/分数, 试验、狗、人和成员表只是元数据 classId 5需要不同的日期范围 这是我对MySQL的查询 select t.id, d.id, p.id, p.firstname, p.lastname, d.id dogId, d.dogN

嗨,我对一个曾经有效的查询有问题。我的SQL技能不是很好,不知道我缺少了什么。或者如果这是正确的方法。也许用临时表代替

基本要点是给定一个特定的时间框架,我需要计算5个班级的最高积分

测试分数-保留分数/分数, 试验、狗、人和成员表只是元数据

classId 5需要不同的日期范围

这是我对MySQL的查询

select
    t.id,
    d.id,
    p.id,
    p.firstname,
    p.lastname,
    d.id dogId,
    d.dogName,
    c.id,
    c.class,
    t.trialStartDate,
    s.points,
    if(c.id = 5, '2012-08-01', '2012-11-18') as startDate,
    if(c.id = 5, '2013-07-31', '2013-12-31') as endDate,
    SUM(ts.points) AS pointsAggregate
from trialScores ts
    inner join trials t on t.id = ts.trialId
    inner join dogs d on d.id = ts.dogId
    inner join people p on p.id = ts.personId
    inner join classes c on c.id = ts.classId
where t.deletedAt is null
    and ts.deletedAt is null
    and ts.memberAtTrial = 1
    and d.omitFromTripleCrownDOY = 0
    and t.associationId = 1           
GROUP BY p.id, ts.dogId, ts.classId
having t.trialStartDate between startDate and endDate
order by ts.classId, pointsAggregate desc
看来这已经解决了问题,在“选择”和“不在组中”中的内容太多了?:

 select
d.dogName,
c.class,
p.firstName,
p.lastName,    
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1 
and t.trialStartDate between if(c.id = 5, '2012-08-01', '2012-11-18') and if(c.id = 5, '2013-07-31', '2013-12-31')  
GROUP BY ts.dogId, ts.classId
order by ts.classId, pointsAggregate desc

你能试试下面的查询并告诉我它是否有效吗

select
d.dogName,
c.class,
p.firstName,
p.lastName,    
SUM(ts.points) AS pointsAggregate
from trialScores ts
inner join trials t on t.id = ts.trialId
inner join dogs d on d.id = ts.dogId
inner join people p on p.id = ts.personId
inner join classes c on c.id = ts.classId
where t.deletedAt is null
and ts.deletedAt is null
and ts.memberAtTrial = 1
and d.omitFromTripleCrownDOY = 0
and t.associationId = 1 
and t.trialStartDate between if(c.id = 5, '2012-08-01', '2012-11-18') and if(c.id = 5, '2013-07-31', '2013-12-31')  
GROUP BY ts.classId,p.firstName,p.lastName 
order by ts.classId, pointsAggregate desc

那么,如果查询出现任何错误,问题是什么?或者你没有得到预期的结果?你能提供一些样本数据和预期的结果吗?对不起,没有错误。。糟糕的结果。如果我手动查找数据并将其进行比较,有些人甚至会从我知道应该存在的结果中忽略。让我看看是否可以设置sqlfiddle,可能需要很长时间才能添加太多数据,但如果John有2个3和4的测试核心,Sue和classId 1的测试核心分别为5和6。比尔的测验分数是0分和15分,玛丽的测验分数是5分和5分。我想要的结果是:1班-苏11,约翰7-2班-比尔15,玛丽10等等…好吧,发生了什么,它给了我每班的第一名。所以我得到了5个结果,每堂课一个,加总积分最高。而总人数/人数远远不够。