Sql 连接三个不同计数的表
我有三张桌子 团队 匹配 得分 我试过这个,但没能得到Sql 连接三个不同计数的表,sql,join,count,Sql,Join,Count,我有三张桌子 团队 匹配 得分 我试过这个,但没能得到 SELECT Team.TeamName, COUNT(h.HomeTeamId) AS TotalMatch, SUM(CASE WHEN h.Match_Status = Team.TeamId THEN 1 ELSE 0 END) AS HomeScore, SUM(CASE WHEN h.Match_WonBy = Team.TeamId OR a.Match_Status= Team.TeamId THEN 1
SELECT Team.TeamName, COUNT(h.HomeTeamId) AS TotalMatch,
SUM(CASE WHEN h.Match_Status = Team.TeamId THEN 1 ELSE 0 END) AS HomeScore,
SUM(CASE WHEN h.Match_WonBy = Team.TeamId OR a.Match_Status= Team.TeamId THEN 1 ELSE 0 END) AS Total
FROM Team RIGHT JOIN Match h
ON Team.TeamId = h.HomeTeamId JOIN Match a
ON Team.TeamId = a.AwayTeamId
GROUP BY Team.TeamName
我需要这个,但我没有得到
仅供参考:这不是家庭作业
在这里,积分是通过3乘以胜利,1乘以平局来计算的。试试这个:
select
t.Team,
count(x.TeamId) played,
sum(coalesce(x.win, 0)) as win,
sum(coalesce(x.loss, 0)) as loss,
count(x.TeamId) - sum(coalesce(x.win, 0)) - sum(coalesce(x.loss, 0)) as draw,
sum(coalesce(x.win, 0)) * 3 + (count(x.TeamId) - sum(coalesce(x.win, 0)) - sum(coalesce(x.loss, 0))) as point
from Team t
left join (
select
HomeTeamId TeamId,
case when Match_WonBy = HomeTeamId then 1 else 0 end win,
case when Match_WonBy = AwayTeamId then 1 else 0 end loss
from Match
union all
select
AwayTeamId TeamId,
case when Match_WonBy = AwayTeamId then 1 else 0 end win,
case when Match_WonBy = HomeTeamId then 1 else 0 end loss
from Match
) x on t.TeamId = x.TeamId
group by t.Team
order by
sum(coalesce(x.win, 0)) * 3 + (count(x.TeamId) - sum(coalesce(x.win, 0)) - sum(coalesce(x.loss, 0))) desc
您如何知道这是一个
绘图
(根据您给我们的架构,column Match_WonBy
的值是多少?没有TeamName
列。此外,您应该用您正在使用的SQL类型来标记问题。@Praveen:draw的值是0。@TimBiegeleisen:TeamName是Team。。刚刚从我的SQL中复制粘贴了查询。borther谢谢您非常感谢你的帮助。
ScoreId MatchId TeamId ScorTime
1 3 1 2/2/2015 12:30:00
2 3 2 2/2/2015 12:35:00
3 3 1 2/2/2015 12:38:00
4 8 1 6/2/2015 12:45:00
5 8 1 6/2/2015 12:49:00
SELECT Team.TeamName, COUNT(h.HomeTeamId) AS TotalMatch,
SUM(CASE WHEN h.Match_Status = Team.TeamId THEN 1 ELSE 0 END) AS HomeScore,
SUM(CASE WHEN h.Match_WonBy = Team.TeamId OR a.Match_Status= Team.TeamId THEN 1 ELSE 0 END) AS Total
FROM Team RIGHT JOIN Match h
ON Team.TeamId = h.HomeTeamId JOIN Match a
ON Team.TeamId = a.AwayTeamId
GROUP BY Team.TeamName
select
t.Team,
count(x.TeamId) played,
sum(coalesce(x.win, 0)) as win,
sum(coalesce(x.loss, 0)) as loss,
count(x.TeamId) - sum(coalesce(x.win, 0)) - sum(coalesce(x.loss, 0)) as draw,
sum(coalesce(x.win, 0)) * 3 + (count(x.TeamId) - sum(coalesce(x.win, 0)) - sum(coalesce(x.loss, 0))) as point
from Team t
left join (
select
HomeTeamId TeamId,
case when Match_WonBy = HomeTeamId then 1 else 0 end win,
case when Match_WonBy = AwayTeamId then 1 else 0 end loss
from Match
union all
select
AwayTeamId TeamId,
case when Match_WonBy = AwayTeamId then 1 else 0 end win,
case when Match_WonBy = HomeTeamId then 1 else 0 end loss
from Match
) x on t.TeamId = x.TeamId
group by t.Team
order by
sum(coalesce(x.win, 0)) * 3 + (count(x.TeamId) - sum(coalesce(x.win, 0)) - sum(coalesce(x.loss, 0))) desc