我想使用CTE计算团队统计数据并交叉申请
calculate team win, loss, home_win, home_loss, highest_win_score,
highest_loss_score for each team.
我的表架构是
Team
表:
Game
表:
结果我想看看:
Team_Name, total_played, win, loss, home_win, home_loss,
highest_win_score, highest_loss_score.
我使用的查询:
With T1(Team_Name, total_played, win, loss, home_win, home_loss,
highest_win_score, highest_loss_score) As
(
Select
A.Team_Name,
B.total_played,
C.win,
C.loss,
B.home_win,
B.home_loss,
D.highest_win_score, D.highest_loss_score
From Team A
CROSS APPLY
(
Select
total_played = count(B.home_team_Id)
From Game where home_team_Id = B.
home_team_Id
Group by B.home_team_Id
) B,
CROSS Apply (
Select
Win = count (*) --no idea how to calculate win loss here
) C,
Cross apply (
Select
highest_win_score = -- how to calculate this
) D
答案 0 :(得分:2)
使用条件聚合:
;WITH Cte AS(
SELECT
t.id AS team_id, t.name AS team_name, g.home_team_score AS team_score,
g.guest_team_id AS opponent_team_id, g.guest_team_score AS opponent_team_score,
1 AS home
FROM Team t
INNER JOIN Game g
ON g.home_team_id = t.id
UNION ALL
SELECT
t.id AS team_id, t.name AS team_name, g.guest_team_score AS team_score,
g.home_team_id AS opponent_team_id, g.home_team_score AS opponent_team_score,
0 AS home
FROM Team t
INNER JOIN Game g
ON g.guest_team_id = t.id
)
SELECT
t.id,
t.name,
home_win = SUM(CASE WHEN c.home = 1 AND c.team_score > c.opponent_team_score THEN 1 ELSE 0 END),
home_loss = SUM(CASE WHEN c.home = 1 AND c.team_score < c.opponent_team_score THEN 1 ELSE 0 END),
guest_win = SUM(CASE WHEN c.home = 0 AND c.team_score > c.opponent_team_score THEN 1 ELSE 0 END),
guest_loss = SUM(CASE WHEN c.home = 0 AND c.team_score < c.opponent_team_score THEN 1 ELSE 0 END),
win_count = SUM(CASE WHEN c.team_score > c.opponent_team_score THEN 1 ELSE 0 END),
loss_count = SUM(CASE WHEN c.team_score < c.opponent_team_score THEN 1 ELSE 0 END),
highest_win_score = MAX(CASE WHEN c.team_score > c.opponent_team_score THEN team_score END),
highest_loss_score = MAX(CASE WHEN c.team_score < c.opponent_team_score THEN team_score END)
FROM Team t
LEFT JOIN Cte c
ON c.team_id = t.id
GROUP BY t.id, t.name
使用APPLY
:
SELECT
t.id,
t.name,
home_win = SUM(CASE WHEN c.home = 1 AND c.team_score > c.opponent_team_score THEN 1 ELSE 0 END),
home_loss = SUM(CASE WHEN c.home = 1 AND c.team_score < c.opponent_team_score THEN 1 ELSE 0 END),
guest_win = SUM(CASE WHEN c.home = 0 AND c.team_score > c.opponent_team_score THEN 1 ELSE 0 END),
guest_loss = SUM(CASE WHEN c.home = 0 AND c.team_score < c.opponent_team_score THEN 1 ELSE 0 END),
win_count = SUM(CASE WHEN c.team_score > c.opponent_team_score THEN 1 ELSE 0 END),
loss_count = SUM(CASE WHEN c.team_score < c.opponent_team_score THEN 1 ELSE 0 END),
highest_win_score = MAX(CASE WHEN c.team_score > c.opponent_team_score THEN team_score END),
highest_loss_score = MAX(CASE WHEN c.team_score < c.opponent_team_score THEN team_score END)
FROM Team t
CROSS APPLY(
SELECT
t.id AS team_id, t.name AS team_name, g.home_team_score AS team_score,
g.guest_team_id AS opponent_team_id, g.guest_team_score AS opponent_team_score,
1 AS home
FROM Game g
WHERE g.home_team_id = t.id
UNION ALL
SELECT
t.id AS team_id, t.name AS team_name, g.guest_team_score AS team_score,
g.home_team_id AS opponent_team_id, g.home_team_score AS opponent_team_score,
0 AS home
FROM Game g
WHERE g.guest_team_id = t.id
)c
GROUP BY t.id, t.name
样本数据
CREATE TABLE Team(id INT, name VARCHAR(5))
CREATE TABLE Game(id INT, [date] DATE, home_team_id INT, guest_team_id INT, home_team_score INT, guest_team_score INT)
INSERT INTO Team VALUES
(1, 'TeamA'), (2, 'TeamB'), (3, 'TeamC'), (4, 'TeamD'), (5, 'TeamE');
INSERT INTO Game VALUES
(1, '20150101', 1, 2, 3, 0),
(2, '20150102', 1, 3, 0, 2),
(3, '20150103', 1, 4, 4, 2),
(4, '20150104', 1, 5, 3, 2),
(5, '20150105', 5, 1, 1, 2),
(6, '20150106', 4, 1, 4, 2),
(7, '20150107', 3, 1, 1, 2),
(8, '20150108', 2, 1, 5, 2);
<强> RESULT 强>
id name home_win home_loss guest_win guest_loss win_count loss_count highest_win_score highest_loss_score
----------- ----- ----------- ----------- ----------- ----------- ----------- ----------- ----------------- ------------------
1 TeamA 3 1 2 2 5 3 4 2
2 TeamB 1 0 0 1 1 1 5 0
3 TeamC 0 1 1 0 1 1 2 1
4 TeamD 1 0 0 1 1 1 4 2
5 TeamE 0 1 0 1 0 2 NULL 2
答案 1 :(得分:0)
试试这个
select
Name as Team_Name,
count(Game.*) as total_played,
sum(case when team_score>opponent_score then 1 else 0 end ) as win,
sum(case when team_score<opponent_score then 1 else 0 end ) as loss,
sum(case when team_score>opponent_score and home=1 then 1 else 0 end ) as home_win,
sum(case when team_score<opponent_score and home=1 then 1 else 0 end ) as home_loss,
max(case when team_score>opponent_score then team_score else 0 end ) as highest_win_score,
max(case when team_score<opponent_score then team_score else 0 end ) as highest_loss_score
from Team
left join
(
select
home_team_Id team_id,
home_team_score team_score,
guest_team_score opponent_score,
1 as 'Home'
from Game as HomeGame
union
select
guest_team_id team_id,
guest_team_score team_score,
home_team_score opponent_score,
0 as 'Home'
from Game as GuestGame
) Game
on Team.id=Game.team_id
group by Name