簡體   English   中英

在SQL中使用交叉應用的團隊贏/輸

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

我想使用CTE計算團隊統計信息並交叉應用於

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

我的表架構是

Team表:

  • ID
  • 名稱

Game桌:

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

結果我想看:

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

使用條件聚合:

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

使用APPLY

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

樣本數據

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);

結果

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

試試這個

 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