sql选择计算

2ul0zpep  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(324)

下面是我正在使用的东西
拉曼的棒球-数据库
数据库的2015 sql版本。
microsoft sql server management studio v17.7
我使用“洛杉矶道奇”的统计数据,我的基础上我的网站选择,并从上述数据库网站的espn洛杉矶道奇拉
我正在尝试用7个单独的select语句重新创建这个网站。我的第二个select语句有问题,第二个select语句的目的是在底部提供一个totals行,就像它们在网站上一样。
(所以我的主要问题是有人能帮我修复第二个选择。)
(我的第二个问题是,在创建与下图对应的其他5条selects语句时,我能否获得一些帮助。)

(1)主体选择:

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

单击此处-选择以上输出的图像
(2)全选:

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'

这是我的全选返回和它的错误。

下面是当我运行select for totals时它应该返回什么我不知道我做错了什么。

d4so4syb

d4so4syb1#

只查看列的总值 BA 很明显,您所做的计算与它们不同,您应该计算列和之间的比率,而不是计算平均值 H 以及 AB 所以更换

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

SUM(H) / SUM(AB) AS 'BA'

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

相关问题