我需要帮助制作一个梯子。
见下信息:
表:
cricket_teams(id, name)
cricket_matches(id, hometeam, awayteam, format, season, winner)
cricket_teamperformance(id, matchid, team, innings, score)
(表中还有其他字段对此查询不必要)
我需要得到它,以便它给我胜利,抽奖(胜利者= 0),失败, FIP (当团队在第一次得分更多时)局), BP (在这种情况下只是获胜)和积分(以粗体显示)。我有一个查询,但我有一些问题。我无法弄清楚如何获得平局和失利,而且也没有让球队没有赢得比赛。这就是我所拥有的:
SELECT b.team,
b.name,
COUNT(c.wins) as W,
count(b.name)*2 as FIP,
COUNT(c.wins)*2 as BP,
COUNT(c.wins)*4+(count(b.name)*2)+(COUNT(c.wins)*2) as Pts
FROM (SELECT a.name, a.score, a.matchid, a.team
FROM (SELECT cricket_teams.name, score, matchid, team
FROM `cricket_teamperformance`
LEFT JOIN cricket_teams
ON cricket_teamperformance.team = cricket_teams.id
INNER JOIN cricket_matches
ON cricket_teamperformance.matchid = cricket_matches.id
WHERE cricket_matches.format=3
AND cricket_teamperformance.innings = 1
AND cricket_matches.season = 1
OR cricket_matches.format=3
AND cricket_teamperformance.innings = 2
AND cricket_matches.season = 1
ORDER BY matchid, score DESC) as a
GROUP BY matchid) as b,
(SELECT COUNT(winner) as wins, cricket_teams.name
FROM cricket_matches
LEFT JOIN cricket_teams ON cricket_matches.winner = cricket_teams.id
WHERE format = 3
AND season = 1) as c
GROUP BY b.name
ORDER BY Pts DESC
然后返回:
team name W FIP BP Pts
4 Chargers 2 4 4 16
2 Hawks 1 2 2 8
1 Ninjas 1 2 2 8
它需要看起来像这样(P是比赛,D是平局,L是亏损):
team name P W D L FIP BP Pts
4 Chargers 2 2 0 0 4 4 16
2 Hawks 2 1 0 1 2 2 8
1 Ninjas 2 1 0 1 2 2 8
3 Wolves 2 0 0 2 0 0 0
还有另一支球队,狼队,他们两场比赛都没有赢过。我还需要检索那些抽奖和损失。提前致谢
*这是多日板球,意味着每场比赛最多可以有两局。 SQL小提琴:http://sqlfiddle.com/#!2/26e41/2
答案 0 :(得分:1)
我发现问题描述和表格架构非常混乱,但这可能会以某种方式帮助您:
SELECT ct.id AS TeamID,
ct.name AS TeamName,
COUNT(DISTINCT cm.id) AS MatchesPlayed,
COUNT(DISTINCT CASE WHEN cm.winner = ct.id THEN cm.id END) AS Wins,
COUNT(DISTINCT CASE WHEN cm.winner = 0 THEN cm.id END) AS Draws,
COUNT(DISTINCT CASE WHEN cm.winner != 0 AND cm.winner != ct.id THEN cm.id END) AS Losses,
COUNT(DISTINCT
CASE
WHEN (ctph.innings = 1 OR ctpa.innings = 1)
AND (ctph.team = ct.id AND ctph.score > ctpa.score)
THEN ctph.matchid
WHEN (ctph.innings = 1 OR ctpa.innings = 1)
AND (ctpa.team = ct.id AND ctpa.score > ctph.score)
THEN ctpa.matchid
END) AS FIP
FROM cricket_teams AS ct
LEFT JOIN cricket_matches AS cm ON cm.hometeam = ct.id OR cm.awayteam = ct.id
LEFT JOIN cricket_teamperformance AS ctph ON ctph.team = cm.hometeam AND ctph.matchid = cm.id
LEFT JOIN cricket_teamperformance AS ctpa ON ctpa.team = cm.awayteam AND ctpa.matchid = cm.id
GROUP BY ct.id, ct.name
答案 1 :(得分:1)
这是另一种解决方案:
SELECT team,
name,
count(*) AS p,
sum(win) AS w,
sum(draw) AS d,
sum(loss) AS l,
sum(fi)*2 AS fip,
sum(win)*2 AS bp,
sum(win)*4 + sum(fi)*2 + sum(win)*2 AS pts
FROM
(SELECT t.id AS team,
t.name,
m.winner = t.id AS win,
m.winner = 0 AS draw,
m.winner <> t.id AND m.winner <> 0 as loss,
CASE
WHEN p1.score > p2.score THEN p1.team = t.id
ELSE p2.team = t.id
END AS fi
FROM cricket_matches m,
cricket_teamperformance p1,
cricket_teamperformance p2,
cricket_teams t
WHERE m.id = p1.matchid
AND m.id = p2.matchid
AND p1.innings = 1
AND p2.innings = 2
AND (t.id = m.hometeam
OR t.id = m.awayteam)
AND m.season = 1
AND m.format = 3) tallies
GROUP BY team
ORDER BY pts DESC, name