我有一个排名查询,可以对团队在挑战中的表现进行排名。
数据的层次结构如下: 团队有成员 成员有活动 活动有活动类型 挑战有活动类型
如果我想在一个挑战中对所有团队的表现进行排名,这个查询效果很好:
SELECT t.teamID, t.teamName,
scoring.challengeID,
outerchallenge.name AS ChallengeName, outerchallenge.description AS ChallengeDescription, outerchallenge.startDate, outerchallenge.endDate,
scoring.standardValueSum, scoring.standardUnit, scoring.rank
FROM challenge outerchallenge
LEFT JOIN (
SELECT teamID, challengeID, standardValueSum, standardUnit, FIND_IN_SET(standardValueSum, scores ) AS rank
FROM (
SELECT teammember.teamID, mc.challengeID, sum(v.standardValue) standardValueSum, v.standardUnit
FROM v_activitystats v
INNER JOIN memberchallenge mc ON v.memberID = mc.memberID AND v.standardValue > 0
INNER JOIN teammember ON v.memberID = teammember.memberID
INNER JOIN challenge c ON mc.challengeID = c.challengeID
INNER JOIN challengeactivitytype cat ON c.challengeID = cat.challengeID AND cat.activityTypeID = v.activityTypeID
WHERE v.activityDate BETWEEN c.startDate AND c.endDate
AND c.challengeID = 33
GROUP BY standardUnit, challengeID, teamID
) vstats
CROSS JOIN (
SELECT GROUP_CONCAT( DISTINCT standardValueSum ORDER BY standardValueSum DESC ) AS scores
FROM (
SELECT teammember.teamID, mc.challengeID, sum(v.standardValue) standardValueSum
FROM v_activitystats v
INNER JOIN memberchallenge mc ON v.memberID = mc.memberID AND v.standardValue > 0
INNER JOIN teammember ON v.memberID = teammember.memberID
INNER JOIN challenge c ON mc.challengeID = c.challengeID
INNER JOIN challengeactivitytype cat ON c.challengeID = cat.challengeID AND cat.activityTypeID = v.activityTypeID
WHERE v.activityDate BETWEEN c.startDate AND c.endDate
AND c.challengeID = 33
GROUP BY challengeID, teamID
) vstats
) scores
) scoring
ON outerchallenge.challengeID = scoring.challengeID
INNER JOIN team t ON scoring.teamID = t.teamID
这是一个格式化查询:http: //mysql.pastebin.com/XggRL5kX
ChallengeID,团队,排名 99 红队 1 99 蓝队 2
再一次,这对于特定的挑战非常有效,(ID = 33)
我想获得具有相同排名的查询,但要针对多个挑战,例如已经结束的挑战。
我试过这个查询:
SELECT rankings.teamID, stuff.teamName, rankings.challengeID,
rankings.ChallengeName, rankings.ChallengeDescription, rankings.startDate, rankings.endDate,
rankings.standardValueSum, rankings.standardUnit, rankings.rank
FROM challenge chal
LEFT JOIN (
SELECT t.teamID, t.teamName, scoring.challengeID,
outerchallenge.name AS ChallengeName, outerchallenge.description AS ChallengeDescription, outerchallenge.startDate, outerchallenge.endDate,
scoring.standardValueSum, scoring.standardUnit, scoring.rank
FROM challenge outerchallenge
LEFT JOIN (
SELECT teamID, challengeID, standardValueSum, standardUnit, FIND_IN_SET(standardValueSum, scores ) AS rank
FROM (
SELECT teammember.teamID, mc.challengeID, sum(v.standardValue) standardValueSum, v.standardUnit
FROM v_activitystats v
INNER JOIN memberchallenge mc ON v.memberID = mc.memberID AND v.standardValue > 0
INNER JOIN teammember ON v.memberID = teammember.memberID
INNER JOIN challenge c ON mc.challengeID = c.challengeID
INNER JOIN challengeactivitytype cat ON c.challengeID = cat.challengeID AND cat.activityTypeID = v.activityTypeID
WHERE v.activityDate BETWEEN c.startDate AND c.endDate
GROUP BY standardUnit, challengeID, teamID ) vstats
CROSS JOIN (
SELECT GROUP_CONCAT( DISTINCT standardValueSum ORDER BY standardValueSum DESC ) AS scores
FROM (
SELECT teammember.teamID, mc.challengeID, sum(v.standardValue) standardValueSum
FROM v_activitystats v
INNER JOIN memberchallenge mc ON v.memberID = mc.memberID AND v.standardValue > 0
INNER JOIN teammember ON v.memberID = teammember.memberID
INNER JOIN challenge c ON mc.challengeID = c.challengeID
INNER JOIN challengeactivitytype cat ON c.challengeID = cat.challengeID AND cat.activityTypeID = v.activityTypeID
WHERE v.activityDate BETWEEN c.startDate AND c.endDate
GROUP BY challengeID, teamID
) vstats
) scores
) scoring ON outerchallenge.challengeID = scoring.challengeID
INNER JOIN team t ON scoring.teamID = t.teamID
) rankings ON chal.challengeID = rankings.challengeID
WHERE chal.endDate <= current_date()
这是一个格式化查询:http: //mysql.pastebin.com/mSZwtDm3
但是,并非每个挑战都有第一名和第二名,而是所有挑战的排名。像这样
ChallengeID,团队,排名 99 红队 1 99 蓝队 2 134 红队 3 134 蓝队 4 443 红队 5 442 蓝队 6
所以,我想,我在错误的地方评估排名,但我对如何完成这项工作有些想法。我怎样才能得到这样的结果: ChallengeID,团队,排名 99 红队 1 99 蓝队 2 134 红队 1 134 蓝队 2 443 红队 1 443 蓝队 2