有人能告诉我我的敲击百分比查询有什么问题吗?

时间:2014-02-26 19:33:37

标签: mysql sql mysql-workbench

我正在试图弄清楚为什么我的总基数和阻塞查询(它们被构建到主查询中)给了我夸大的结果。我在一个单独的查询中运行每一个,我得到的数字更接近我想要的(但不正确)。你需要一些基本的棒球知识来帮助我!

Select 
      bbat.playerID, 
      bmast.firstname, 
      bmast.lastname, 
      bbat.yearID, 
      bbat.TeamID, 
      bmast.bats, 
      bmast.throws, 
      bfield.POS, 
      bbat.G, 
      bbat.G_batting, 
      bbat.AB, 
      bbat.R, 
      bbat.H, 
      bbat.2B, 
      bbat.3B, 
      bbat.HR, 
      bbat.RBI, 
      bbat.HBP, 
      bbat.SF, 
      (Sum(H)+(Sum(2B)*2)+(Sum(3B)*3)+(Sum(HR)*4)) as 'TB',
      bbat.BB, 
      bbat.SO, 
      (Sum(BB)/Sum(SO)) as 'BB/K', 
      (SUM(H)/Sum(AB)) as 'Avg',
      ((Sum(H)+Sum(BB)+Sum(HBP))/(Sum(AB)+Sum(BB)+Sum(HBP)+Sum(SF))) as 'OBP', 
      (Sum(H)+(Sum(2B)*2)+(Sum(3B)*3)+(Sum(HR)*4))/Sum(AB) as 'Slugging', 
      (((Sum(H)+Sum(BB)+Sum(HBP))/(Sum(AB)+Sum(BB)+Sum(HBP)+Sum(SF)))+((Sum(H)+(Sum(2B)*2)+(Sum(3B)*3)+(Sum(HR)*4))/Sum(AB))) as 'OPS',
      bbat.SB, 
      bbat.CS, 
      (Sum(SB)/(Sum(SB)+Sum(CS))) as 'Stolen Base %', 
      ((Sum(H)+Sum(BB))*(Sum(H)+(Sum(2B)*2)+(Sum(3B)*3)+(Sum(HR)*4)))/(Sum(AB)+Sum(BB)) as 'Runs Created'
   from 
      baseball.batting bbat 
         inner join ( Select PlayerID, 
                             min(nameFirst) as 'firstname', 
                             min(nameLast) as 'lastname', 
                             bats,
                             throws 
                         from 
                            baseball.master
                         group by 
                            playerID, 
                            nameFirst, 
                            nameLast ) bmast
            on bbat.playerID = bmast.PlayerID
         inner join ( Select PlayerID, 
                             POS
                          from baseball.fielding ) bfield
            on bbat.playerID = bfield.playerID
   where 
          yearID = '2013' 
      and AB > 60 
      and POS != 'P'
   Group by 
      bmast.playerID, 
      bmast.firstname, 
      bmast.lastname;

如果您愿意,我可以发布我的个人查询。请让我知道!

2 个答案:

答案 0 :(得分:1)

虽然MySQL允许您从GROUP BY中排除非聚合字段,但它可能会产生不需要的结果。起点包括SELECTGROUP BY列表中的所有非汇总字段。您的查询可能还有其他问题。

更新:此子查询存在问题,您正在获取两个值的MIN(),但也按它们分组(这会取消MIN(),并且您不会按击球/投掷进行分组。

 Select PlayerID, 
                             min(nameFirst) as 'firstname', 
                             min(nameLast) as 'lastname', 
                             bats,
                             throws 
                         from 
                            baseball.master
                         group by 
                            playerID, 
                            nameFirst, 
                            nameLast ) bmast

答案 1 :(得分:0)

我猜你在找这个

 Select 
  bbat.playerID, 
  bmast.firstname, 
  bmast.lastname, 
  bbat.yearID, 
  bbat.TeamID, 
  bmast.bats, 
  bmast.throws, 
  bfield.POS, 
  bbat.G, 
  bbat.G_batting, 
  bbat.AB, 
  bbat.R, 
  bbat.H, 
  bbat.2B, 
  bbat.3B, 
  bbat.HR, 
  bbat.RBI, 
  bbat.HBP, 
  bbat.SF, 
  ((H)+((2B)*2)+((3B)*3)+((HR)*4)) as 'TB',
  bbat.BB, 
  bbat.SO, 
  ((BB)/(SO)) as 'BB/K', 
  ((H)/(AB)) as 'Avg',
  (((H)+(BB)+(HBP))/((AB)+(BB)+(HBP)+(SF))) as 'OBP', 
  ((H)+((2B)*2)+((3B)*3)+((HR)*4))/(AB) as 'Slugging', 
  ((((H)+(BB)+(HBP))/((AB)+(BB)+(HBP)+(SF)))+(((H)+((2B)*2)+((3B)*3)+((HR)*4))/(AB))) as 'OPS',
  bbat.SB, 
  bbat.CS, 
  ((SB)/((SB)+(CS))) as 'Stolen Base %', 
  (((H)+(BB))*((H)+((2B)*2)+((3B)*3)+((HR)*4)))/((AB)+(BB)) as 'Runs Created'
 from 
 ....