联合子查询问题

时间:2015-03-09 00:45:01

标签: mysql sql subquery union union-all

我尝试使用union sub查询来获取几个不同查询的结果。我要做的就是选择所有在2014赛季击出本垒打的球员,为每位球员创造一个本垒打计数并找到每个本垒打的平均球速。我也试图按音调类型分解,我当前的代码和结果如下:

Select output.Batter_Name,
output.Qty,
output.speed,
output.avg_Speed,
output.break,
output.Type_Pitch,
    Output.CH_Qty,
    Output.CH_Pitch,
    Output.Ch_Speed,
    Output.CH_Avg_speed,
    Output.CH_Break,
    Output.CH_Type_Pitch

From(
SELECT 
    count(gameday.atbats.event) as Qty,
    gameday.batters.name_display_first_last as Batter_Name,
    gameday.pitches.type as Pitch,
    gameday.pitches.start_speed as speed,
    avg(gameday.pitches.start_speed) as avg_speed,
    avg(gameday.pitches.break_length) as Break,
    gameday.pitches.Pitch_type as Type_Pitch,
    "0" as CH_Qty,
    "0" as CH_Pitch,
    "0" as Ch_Speed,
    "0" as CH_Avg_speed,
    "0" as CH_Break,
    "0" as CH_Type_Pitch

FROM


    gameday.atbats
        JOIN
    gameday.pitches ON gameday.atbats.num = gameday.pitches.gameAtBatID
        AND gameday.pitches.gamename = gameday.atbats.gamename
        INNER JOIN
    gameday.batters ON gameday.atbats.batter = gameday.batters.ID
        AND gameday.atbats.gamename = gameday.batters.gameName
        INNER JOIN
    gameday.pitchers ON gameday.atbats.pitcher = gameday.pitchers.ID
        AND gameday.atbats.gamename = gameday.pitchers.gamename
WHERE
    (gameday.atbats.event = 'Home Run')
        AND gameday.pitches.type = 'x'
        and gameday.pitches.Pitch_type = 'FF'
 group by gameday.batters.name_display_first_last

 UNION ALL

 SELECT 
    "0" as Qty,
    gameday.batters.name_display_first_last as Batter_Name,
    "0" as Pitch,
    "0" as Speed,
    "0" as Avg_speed,
    "0" as Break,
    "0" as Type_Pitch,
    count(gameday.atbats.event) as CH_Qty,
    gameday.pitches.type as CH_Pitch,
    gameday.pitches.start_speed as CH_speed,
    avg(gameday.pitches.start_speed) as CH_avg_speed,
    avg(gameday.pitches.break_length) as CH_Break,
    gameday.pitches.Pitch_type as CH_Type_Pitch


FROM


    gameday.atbats
        JOIN
    gameday.pitches ON gameday.atbats.num = gameday.pitches.gameAtBatID
        AND gameday.pitches.gamename = gameday.atbats.gamename
        INNER JOIN
    gameday.batters ON gameday.atbats.batter = gameday.batters.ID
        AND gameday.atbats.gamename = gameday.batters.gameName
        INNER JOIN
    gameday.pitchers ON gameday.atbats.pitcher = gameday.pitchers.ID
        AND gameday.atbats.gamename = gameday.pitchers.gamename
WHERE
    (gameday.atbats.event = 'Home Run')
        AND gameday.pitches.type = 'x'
        and gameday.pitches.Pitch_type = 'CH'
 group by gameday.batters.name_display_first_last
 ) as Output

 group by Output.Batter_name

我的结果示例如下:

Batter_Name, Qty, speed, avg_Speed, break, Type_Pitch, CH_Qty, CH_Pitch, Ch_Speed, CH_Avg_speed, CH_Break, CH_Type_Pitch
A.J. Pollock    1     89     90      4.3      FF        0        0          0           0          0         0
Aaron Hicks     0      0      0      0        0         1        X          83          83         6          CH

第一名球员埃利斯表示他在FF上有一个本垒打,在CH上有零。第二名球员Peirzynski在FF上有0个本垒打,但在CH上有1个。问题是我知道这些球员在这两种球场上都有本垒打,但查询只是一个或另一个,而不是两者。我的预期结果是这样的:

Batter_Name, Qty, speed, avg_Speed, break, Type_Pitch, CH_Qty, CH_Pitch, Ch_Speed, CH_Avg_speed, CH_Break, CH_Type_Pitch
A.J. Pollock    1     89     90      4.3      FF        2        X          84           82         3.2          CH
Aaron Hicks     4     90     91      2.5      FF        1        X          83           83          6           CH

我认为问题必须是我将某些字段设置为0,有点像占位符,但我似乎无法找到一个可行的解决方案来获取我想要的结果。

0 个答案:

没有答案