遇到WHERE子句

时间:2019-03-16 22:39:12

标签: sql sqlite

我试图获取多个条件的结果,但是在尝试将两个查询合并为一个时遇到麻烦。我希望获得以下类别的迈阿密热火队获胜率的平均值+以下类别中纽约尼克斯队失利的平均值,并将它们合并为一个平均值。

这是我对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  从box_score  WHERE team_name =“迈阿密热火” 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”

在那条线之前,其他所有内容都是相同的...我的问题是尝试将两者合并为一个查询,在此我得到热火队获胜的平均值和尼克斯队损失的平均值。我认为问题出在where子句中,但我无法弄清楚,我只能得到空结果...所有这些信息都来自同一张表,我可以从ID号或通过名称...我正在使用SQLite,如果那改变了什么

这是运行查询的结果,这是我正在寻找具有平均值的一行数据。但是,我希望将热火和尼克斯输球中这些数字的平均值合并为一行该查询仅显示热火胜利的平均值。

平均胜率

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

我想将每个类别的两者合计为1个平均值

1 个答案:

答案 0 :(得分:1)

使用聚合:

SELECT bs.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 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') 
      )
GROUP BY bs.team_name;

我还建议您在WIN_LOSSSELECT中包括GROUP BY,因此它也在结果集中。

编辑:

根据您的评论,只需删除GROUP BY

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