SQL选择计算

时间:2018-06-18 06:58:16

标签: mysql sql sql-server sql-server-2008 sql-server-2005

  

以下是我使用的内容

     

我尝试使用7个单独的选择语句重新创建网站。我目前在我的第二个选择陈述中遇到问题,第二个目的是在底部提供一个总计行,就像在网站上一样。

  

(所以我的主要问题是有人可以帮我修复我的第二个选择。)

     

(我的第二个问题是我可以在创建另外5个问题时得到一些帮助   选择与下图对应的语句。)

Click for 5 selects image

(第一) 主体选择

SELECT
  nameFirst + ' ' + nameLast AS Name,
  G AS GP,
  AB,
  R,
  H,
  H - B2 - B3 - HR AS S,
  B2 AS '2B',
  B3 AS '3B',
  HR,
  RBI,
  (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) AS 'TB',
  BB,
  SO,
  SB,
  (H * 1.0) / (CASE
    WHEN AB = 0 THEN 1
    ELSE AB
  END) AS 'BA',
  (CASE
    WHEN (H + BB + HBP) = 0 THEN 0
    ELSE ((H + BB + HBP) * 1.0)
  END) / (CASE
    WHEN (AB + BB + HBP) = 0 THEN 1.0
    ELSE ((AB + BB + HBP) * 1.0)
  END) AS 'OBP',
  (CASE
    WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
    ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
  END) / (CASE
    WHEN AB = 0 THEN 1
    ELSE AB
  END) AS 'SLG',
  (CASE
    WHEN (H + BB + HBP) = 0 THEN 0
    ELSE ((H + BB + HBP) * 1.0)
  END) / (CASE
    WHEN (AB + BB + HBP) = 0 THEN 1.0
    ELSE ((AB + BB + HBP) * 1.0)
  END) +
  (CASE
    WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
    ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
  END) / (CASE
    WHEN AB = 0 THEN 1
    ELSE AB
  END) AS 'OPS'
FROM Players
LEFT OUTER JOIN Batting
  ON Players.playerIDpk = Batting.playerID
WHERE teamID = 'LAN'
AND yearID = '2012'
ORDER BY BA DESC

Click Here - For Image of above select output

(第二) 总选择次数:

SELECT
  '' AS Total,
  '162' AS 'GP',
  SUM(AB) AS 'AB',
  SUM(R) AS 'R',
  SUM(H) AS 'H',
  SUM(B2) AS '2B',
  SUM(B3) AS '3B',
  SUM(HR) AS HR,
  SUM(RBI) AS RBI,
  SUM((((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))) AS 'TB',
  SUM(BB) AS BB,
  SUM(SO) AS 'SO',
  SUM(SB) AS 'SB',
  AVG((H * 1.0) / (CASE
    WHEN AB = 0 THEN 1
    ELSE AB
  END)) AS 'BA',
  AVG((CASE
    WHEN (H + BB + HBP) = 0 THEN 0
    ELSE ((H + BB + HBP) * 1.0)
  END) / (CASE
    WHEN (AB + BB + HBP) = 0 THEN 1.0
    ELSE ((AB + BB + HBP) * 1.0)
  END)) AS 'OBP',
  AVG((CASE
    WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
    ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
  END) / (CASE
    WHEN AB = 0 THEN 1
    ELSE AB
  END)) AS 'SLG',
  AVG((CASE
    WHEN (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4)) = 0 THEN 0
    ELSE (((H - B2 - B3 - HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR * 4))
  END) / (CASE
    WHEN AB = 0 THEN 1
    ELSE AB
  END)) AS 'OPS'
FROM Players
LEFT OUTER JOIN Batting
  ON Players.playerIDpk = Batting.playerID
WHERE teamID = 'LAN'
AND yearID = '2012'
  

这是我的总选择返回的内容和不正确的内容。

Incorrect Images

  

以下是我在运行总计选择时应返回的内容我不知道自己做错了什么。

Correct Image

1 个答案:

答案 0 :(得分:0)

仅查看列BA的总值,显然您没有进行与它们相同的计算,而不是计算平均值,您应该计算列之和的比率{{1 }和H

所以替换

AB

AVG((H * 1.0) / (CASE
  WHEN AB = 0 THEN 1
  ELSE AB
END)) AS 'BA',

我没有检查其他不正确的列,但我认为这是同样的问题。