简体   繁体   English

在SQL中使用交叉应用的团队赢/输

[英]Team win/loss using cross apply in SQL

I would like to calculate team stats using a CTE and cross apply to 我想使用CTE计算团队统计信息并交叉应用于

calculate team win, loss, home_win, home_loss, highest_win_score,
highest_loss_score for each team.

My table schema is 我的表架构是

Team table: Team表:

  • id ID
  • Name 名称

Game table: Game桌:

  • id ID
  • date 日期
  • home_team_Id home_team_Id
  • guest_team_id guest_team_id
  • home_team_score home_team_score
  • guest_team_score guest_team_score

Result I would like to see: 结果我想看:

Team_Name, total_played, win, loss, home_win, home_loss,
highest_win_score, highest_loss_score.

Query I used: 我使用的查询:

    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

Using conditional aggregation: 使用条件聚合:

SQL Fiddle SQL小提琴

;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

Using APPLY : 使用APPLY

SQL Fiddle SQL小提琴

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

Sample Data 样本数据

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

Try this one 试试这个

 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM