相关子查询?从同一列的不同列中提取数据

时间:2019-03-17 23:28:51

标签: sql sqlite inner-join correlated-subquery

我正在尝试使用多个条件从不同的列中提取数据,但在弄清楚如何处理时,我认为这是我需要的相关子查询,并且尝试了几种不同的方法,但无法弄清楚。

我希望获得以下类别的迈阿密热火冠军的平均值+以下类别的纽约尼克斯队损失平均值,并将它们合并为一个平均值。

这是我对Heat的查询,它精确地检索了我想要的内容

SELECT
    box_score.team_name, 
    ROUND(AVG(eFG),3) eFG,
    ROUND(AVG(OPP_eFG),3) OPP_eFG,
    ROUND(AVG(TOV_PCT),3) TOV_PCT,
    ROUND(AVG(OPP_TOV_PCT),3) OPP_TOV_PCT,
    ROUND(AVG(ORB_PCT),3) ORB_PCT,
    ROUND(AVG(DRB_PCT),3) DRB_PCT,
    ROUND(AVG(FTA_RATE),3) FTA_RATE,
    ROUND(AVG(OPP_FTA_RATE),3) OPP_FTA_RATE
FROM box_score
WHERE team_name = 'Miami Heat' AND WIN_LOSS = 'W' AND game_date < '2019-03-07' 

我也为尼克斯付出了损失,这也导致了我想要的

WHERE team_name = 'New York Knicks' AND WIN_LOSS = 'L' AND game_date < '2019-03-07' 

我的问题是尝试将两者合并为一个查询,以获取热火队获胜的平均数和尼克斯队损失的平均数。所有这些信息都来自同一张表,我可以从ID号或姓名中获取团队信息...如果使用的任何内容都在改变,我正在使用SQLite

这是运行查询的结果,这是我正在寻找具有平均值的一行数据...但是,我希望将热火胜利和尼克斯损失中的这些数字的平均值合并为一行< / p>

平均胜出

eFG    OPP_eFG  TOV_PCT  OPP_TOV_PCT  ORB_PCT  DRB_PCT  FTA_RATE  OPP_FTA_RATE
0.603  0.505    0.14     0.126        0.28     0.77     0.235     0.141

这些是尼克斯损失的平均值

eFG    OPP_eFG  TOV_PCT  OPP_TOV_PCT  ORB_PCT  DRB_PCT  FTA_RATE  OPP_FTA_RATE
0.568  0.602    0.146    0.136        0.225    0.787    0.222     0.235

我想将每个类别的两个平均值合并为

但是有什么方法可以使我得到平均值以从单独的列中提取数据吗?

在这种情况下,我对迈阿密热火很感兴趣,所以我得到了上面的平均值,但是我想做的是获取热火的平均值到尼克斯对应的相反数据(eFG应该与其他团队的opp_eFG等)...所以基本上我正在寻找以下各项的平均值:

热战eFG和OPP_eFG尼克斯

OPP_eFG和eFG尼克斯热队

热火TOV_PCT和OPP_TOV_PCT尼克斯

OPP_TOV_PCT和TOV_PCT尼克斯热火

热队FTA_RATE和OPP_FTA_RATE尼克斯

OPP_FTA_RATE和FTA_RATE热火尼克斯

仍然希望获得1行作为结果

3 个答案:

答案 0 :(得分:1)

此答案假设您想要的是原始帖子中的AVG(热量)-AVG(尼克斯),而不是AVG(heatsX或knicksY)

我想为此推广通用表表达式:

WITH selector_heat as (
SELECT
    box_score.team_name, 
    ROUND(AVG(eFG),3) eFG,
    ROUND(AVG(OPP_eFG),3) OPP_eFG,
    ROUND(AVG(TOV_PCT),3) TOV_PCT,
    ROUND(AVG(OPP_TOV_PCT),3) OPP_TOV_PCT,
    ROUND(AVG(ORB_PCT),3) ORB_PCT,
    ROUND(AVG(DRB_PCT),3) DRB_PCT,
    ROUND(AVG(FTA_RATE),3) FTA_RATE,
    ROUND(AVG(OPP_FTA_RATE),3) OPP_FTA_RATE
FROM box_score
WHERE team_name = 'Miami Heat' AND WIN_LOSS = 'W' AND game_date < '2019-03-07' 
)
, selector_knicks as (
...
)
select H.eFG - K.OPP_eFG as magic_nbr
from selector_heat H 
join selector_knicks K ON (1=1)

此处提供有关语法的更多详细信息:https://www.sqlite.org/lang_with.html 但是暂时忽略“递归”位,在这种情况下您不需要它们。

或者(可以使用稍微不同的接近角度),您可以使用Window子句汇总“每个团队”,然后使用结果。 此处更多信息:https://www.sqlite.org/windowfunctions.html#introduction_to_window_functions

示例:

SELECT  
  team_name, 
  WIN_LOSS,
  ROUND(AVG(eFG) OVER (partition by team_name, win_loss),3) as eFG
  ...
  from box_score
  where game_date < '2019-03-07'

有了这个结果集,您便可以得出所有团队和win_loss组合的平均值。 将其包装在CTE中,并在适合的条件下加入自身,例如

WITH cte as (SELECT ...)
SELECT H.eFG - K.OPP_eFG as magic_nbr
FROM cte H join cte K 
  ON (H.team_name = 'Miami Heat' 
  AND K.team_name = 'NY Knicks'
  AND H.win_loss = 'W'
  AND K.win_loss = 'L')

答案 1 :(得分:1)

如果要先计算平均值 ,然后对它们进行平均,则可以使用两个聚合级别:

SELECT ROUND(AVG(eFG), 3) as eFG,
       ROUND(AVG(OPP_eFG), 3) as OPP_eFG,
       ROUND(AVG(TOV_PCT), 3) as TOV_PCT,
       ROUND(AVG(OPP_TOV_PCT), 3) as OPP_TOV_PCT,
       ROUND(AVG(ORB_PCT), 3) as ORB_PCT,
       ROUND(AVG(DRB_PCT), 3) as DRB_PCT,
       ROUND(AVG(FTA_RATE), 3) as FTA_RATE,
       ROUND(AVG(OPP_FTA_RATE), 3) as OPP_FTA_RATE
FROM (SELECT bs.team_name, 
             AVG(eFG) as eFG,
             AVG(OPP_eFG) as OPP_eFG,
             AVG(TOV_PCT) as TOV_PCT,
             AVG(OPP_TOV_PCT) as OPP_TOV_PCT,
             AVG(ORB_PCT) as ORB_PCT,
             AVG(DRB_PCT) as DRB_PCT,
             AVG(FTA_RATE) as FTA_RATE,
             AVG(OPP_FTA_RATE) as OPP_FTA_RATE
      FROM box_score bs
      WHERE game_date < '2019-03-07' AND
            ( (team_name = 'Miami Heat' AND WIN_LOSS = 'W') OR
              (team_name = 'New York Knicks' AND WIN_LOSS = 'L')
            )
     ) bs

答案 2 :(得分:0)

一种解决方案是使用条件聚合

,在单个表扫描中执行整个操作(无联接或子查询):
SELECT  
    box_score.team_name, 
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN eFG          END),3) Heat_eFG,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN eFG          END),3) Knicks_eFG,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN OPP_eFG      END),3) Heat_OPP_eFG,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN OPP_eFG      END),3) Knicks_OPP_eFG,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN TOV_PCT      END),3) Heat_TOV_PCT,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN TOV_PCT      END),3) Knicks_TOV_PCT,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN OPP_TOV_PCT  END),3) Heat_OPP_TOV_PCT,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN OPP_TOV_PCT  END),3) Knicks_OPP_TOV_PCT,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN ORB_PCT      END),3) Heat_ORB_PCT,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN ORB_PCT      END),3) Knicks_ORB_PCT,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN DRB_PCT      END),3) Heat_DRB_PCT,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN DRB_PCT      END),3) Knicks_DRB_PCT,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN FTA_RATE     END),3) Heat_FTA_RATE,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN FTA_RATE     END),3) Knicks_FTA_RATE,
    ROUND(AVG(CASE WHEN team_name = 'Miami Heat'      AND WIN_LOSS = 'W' THEN OPP_FTA_RATE END),3) Heat_OPP_FTA_RATE,
    ROUND(AVG(CASE WHEN team_name = 'New York Knicks' AND WIN_LOSS = 'L' THEN OPP_FTA_RATE END),3) Knicks_OPP_FTA_RATE
FROM box_score
WHERE team_name IN ('Miami Heat', 'New York Knicks') AND game_date < '2019-03-07' 

如果您要计算平均值,则这里是查询的另一个版本,例如,在单一列中,eFG代表迈阿密获胜,OPP_eFG代表纽约损失。这仍然依赖于条件聚合。我还通过将条件移至WHERE子句来简化了逻辑。

SELECT  
    box_score.team_name, 
    ROUND(AVG(CASE 
        WHEN team_name = 'Miami Heat'      THEN eFG 
        WHEN team_name = 'New York Knicks' THEN OPP_eFG 
    END, 3) Heats_eFG_Knicks_OPP_eFG, 
    ROUND(AVG(CASE 
        WHEN team_name = 'Miami Heat'      THEN OPP_eFG 
        WHEN team_name = 'New York Knicks' THEN eFG 
    END, 3) Heats_OPP_eFG_Knicks_eFG,
    ROUND(AVG(CASE 
        WHEN team_name = 'Miami Heat'      THEN TOV_PCT 
        WHEN team_name = 'New York Knicks' THEN OPP_TOV_PCT 
    END, 3) Heats_TOV_PCT_Knicks_OPP_TOV_PCT,
    ROUND(AVG(CASE 
        WHEN team_name = 'Miami Heat'      THEN OPP_TOV_PCT 
        WHEN team_name = 'New York Knicks' THEN TOV_PCT 
    END, 3) Heats_OPP_TOV_PCT_Knicks_TOV_PCT,
    ROUND(AVG(CASE 
        WHEN team_name = 'Miami Heat'      THEN FTA_RATE 
        WHEN team_name = 'New York Knicks' THEN OPP_FTA_RATE 
    END, 3) Heats_FTA_RATE_Knicks_OPP_FTA_RATE,
    ROUND(AVG(CASE 
        WHEN team_name = 'Miami Heat'      THEN OPP_FTA_RATE 
        WHEN team_name = 'New York Knicks' THEN FTA_RATE 
    END, 3) Heats_OPP_FTA_RATE_Knicks_FTA_RATE
FROM box_score
WHERE 
    game_date < '2019-03-07' 
    AND (
           ( team_name = 'Miami Heat'      AND win_loss = 'W' )
        OR ( team_name = 'New York Knicks' AND win_loss = 'L') 
    )

NB:正如wildpasser所评论的那样,您可能希望使用单引号而不是双精度值周围的双引号(这是SQL标准)。我在全局范围内将原始查询中的所有双引号都改为单引号。