如何重写SQL查询以获得一个团队的统计数据与对手玩的统计数据?

时间:2017-10-19 08:56:08

标签: mysql

我编写了一个SQL查询来显示特定团队的日程安排以及他们的分数和第一次下降。

SELECT DISTINCT

/*From statsSchedules*/
  homeScore

/*From leagueTeams*/  
, homeTeam.logoId       homeLogoId 
, homeTeam.abbrName     homeAbbrName

/*From statsTeamStats*/
, homeTeamStats.off1stDowns AS home1stDowns


/*From statsSchedules*/
, awayScore

/*From leagueTeams*/
, awayTeam.logoId       awayLogoId 
, awayTeam.abbrName     awayAbbrName

/*From statsTeamStats*/
, awayTeamStats.off1stDowns AS away1stDowns

FROM statsSchedules
INNER JOIN leagueTeams AS homeTeam 
ON statsSchedules.homeTeamId = homeTeam.TeamId


INNER JOIN statsTeamStats AS homeTeamStats
ON statsSchedules.homeTeamId=homeTeamStats.teamId
AND statsSchedules.homeTeamId=homeTeamStats.teamId 
AND statsSchedules.scheduleId=homeTeamStats.scheduleId


INNER JOIN leagueTeams AS awayTeam 
ON statsSchedules.awayTeamId = awayTeam.teamId 

INNER JOIN statsTeamStats AS awayTeamStats
ON statsSchedules.awayTeamId=awayTeamStats.teamId
AND statsSchedules.awayTeamId=awayTeamStats.teamId 
AND statsSchedules.scheduleId=awayTeamStats.scheduleId

WHERE awayTeam.abbrName LIKE 'DAL' 
AND statsSchedules.stageIndex=1 OR homeTeam.abbrName LIKE 'DAL' 
AND statsSchedules.stageIndex=1 
ORDER BY `statsSchedules`.`weekIndex` ASC

该查询输出此..

 |homeScore|homeLogoId|homeAbbrName|home1stDowns|awayScore|awayLogoId|awayAbbrName|away1stDowns|
 |   24    |    10    |    DAL     |     12     |   16    |    15    |    NYG     |      8     |
 |   10    |    3     |    DEN     |     8      |   16    |    10    |    DAL     |      11    |
 |   16    |    6     |    ARI     |     16     |   22    |    10    |    DAL     |      9     |
 |   34    |    10    |    DAL     |     12     |   13    |    23    |    LAR     |      7     |
 |   13    |    10    |    DAL     |     11     |   31    |    19    |    GB      |      8     |

我现在要做的是创建一个新桌面,其中包含DAL的总得分以及对手DAL得分的总得分。

我还希望有一个列显示DAL的1stDowns,另一列显示它的对手所做的第一次降落的总数。

预期

|mainTeamPts|mainTeam1stDowns|opponentsPts|opponents1stDowns|
|   109     |      55        |    86      |      59         |

1 个答案:

答案 0 :(得分:0)

以下是基于上面的小结果表的2个示例,它们可能会为您提供有关如何继续的建议。

在此处展示:SQL Fiddle

MySQL 5.6架构设置

CREATE TABLE QryResult
    (`homeScore` int, `homeLogoId` int, `homeAbbrName` varchar(3), `home1stDowns` int, `awayScore` int, `awayLogoId` int, `awayAbbrName` varchar(3), `away1stDowns` int)
;

INSERT INTO QryResult
    (`homeScore`, `homeLogoId`, `homeAbbrName`, `home1stDowns`, `awayScore`, `awayLogoId`, `awayAbbrName`, `away1stDowns`)
VALUES
    (24, 10, 'DAL', 12, 16, 15, 'NYG', 8),
    (10, 3, 'DEN', 8, 16, 10, 'DAL', 11),
    (16, 6, 'ARI', 16, 22, 10, 'DAL', 9),
    (34, 10, 'DAL', 12, 13, 23, 'LAR', 7),
    (13, 10, 'DAL', 11, 31, 19, 'GB', 8)
;

查询1

select
    teams.AbbrName, r1.homeScore, r1.home1stDowns, r2.awayScore, r2.away1stDowns
from (
      select homeAbbrName AbbrName from QryResult
      union
      select awayAbbrName from QryResult
     ) teams
left join (
          select homeAbbrName, sum(homeScore) homeScore, sum(home1stDowns) home1stDowns
          from QryResult
          group by homeAbbrName
          ) r1 on teams.AbbrName = homeAbbrName
left join (
          select awayAbbrName, sum(awayScore) awayScore, sum(away1stDowns) away1stDowns
          from QryResult
          group by awayAbbrName
          ) r2 on teams.AbbrName = awayAbbrName
order by teams.AbbrName, homeAbbrName

<强> Results

| AbbrName | homeScore | home1stDowns | awayScore | away1stDowns |
|----------|-----------|--------------|-----------|--------------|
|      ARI |        16 |           16 |    (null) |       (null) |
|      DAL |        71 |           35 |        38 |           20 |
|      DEN |        10 |            8 |    (null) |       (null) |
|       GB |    (null) |       (null) |        31 |            8 |
|      LAR |    (null) |       (null) |        13 |            7 |
|      NYG |    (null) |       (null) |        16 |            8 |

查询2

select
    abbrname
  , logoid
  , sum(score) score
  , sum(1stdowns) 1stdowns
from (
      select `homeScore` score, `homeLogoId` logoid, `homeAbbrName` abbrname, `home1stDowns` 1stdowns
      from QryResult
      union all
      select `awayScore`, `awayLogoId`, `awayAbbrName`, `away1stDowns`
      from QryResult
    ) d
GROUP BY
    abbrname
  , logoid
ORDER BY
    abbrname
  , logoid

<强> Results

| abbrname | logoid | score | 1stdowns |
|----------|--------|-------|----------|
|      ARI |      6 |    16 |       16 |
|      DAL |     10 |   109 |       55 |
|      DEN |      3 |    10 |        8 |
|       GB |     19 |    31 |        8 |
|      LAR |     23 |    13 |        7 |
|      NYG |     15 |    16 |        8 |