我有一个关于制作视图的SQL语句....
CREATE VIEW [dbo].[vwPlayersBatting]
AS
SELECT
dbo.Players.playerIDpk, dbo.Players.birthDate,
dbo.Players.birthCountry, dbo.Players.birthState,
dbo.Players.birthCity, dbo.Players.deathDate,
dbo.Players.deathCountry, dbo.Players.deathState,
dbo.Players.deathCity, dbo.Players.nameFirst,
dbo.Players.nameLast, dbo.Players.nameGiven,
dbo.Players.weight, dbo.Players.height,
dbo.Players.bats, dbo.Players.throws,
dbo.Players.debutDate, dbo.Players.finalGameDate,
dbo.Batting.BattingIDPK, dbo.Batting.yearID,
dbo.Batting.stint, dbo.Batting.lgID, dbo.Batting.G,
dbo.Batting.AB, dbo.Batting.R, dbo.Batting.H, dbo.Batting.B2,
dbo.Batting.B3, dbo.Batting.HR, dbo.Batting.RBI, dbo.Batting.SB,
dbo.Batting.CS, dbo.Batting.BB, dbo.Batting.SO, dbo.Batting.IBB,
dbo.Batting.HBP, dbo.Batting.SH, dbo.Batting.SF, dbo.Batting.GIDP,
dbo.Batting.teamID,
(dbo.Batting.H - dbo.Batting.B2 - dbo.Batting.B3 - dbo.Batting.HR) AS S,
(((dbo.Batting.H - dbo.Batting.B2 - dbo.Batting.B3 - dbo.Batting.HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR *4)) AS 'TB',
H*1.0/(Case when AB=0 then 1 else AB end) AS 'BA',
(Case when (H + BB + HBP) = 0 then 1.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 (((dbo.Batting.H - dbo.Batting.B2 - dbo.Batting.B3 - dbo.Batting.HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR *4)) = 0 then 1 else (((dbo.Batting.H - dbo.Batting.B2 - dbo.Batting.B3 - dbo.Batting.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 1.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 (((dbo.Batting.H - dbo.Batting.B2 - dbo.Batting.B3 - dbo.Batting.HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR *4)) = 0 then 1 else (((dbo.Batting.H - dbo.Batting.B2 - dbo.Batting.B3 - dbo.Batting.HR) * 1.0) + (B2 * 2) + (B3 * 3) + (HR *4)) end)/(Case when AB=0 then 1 else AB end) AS 'OPS'
FROM
dbo.Players
INNER JOIN
dbo.Batting ON dbo.Players.playerIDpk = dbo.Batting.playerID
我在公式中拥有它以防止它被零除错误,但它将.000列更改为1.0000,我也无法弄清楚如何使用该列将列舍入到3地方,所以而不是.261904我只有.262,我相信数学错误与在进行数学运算之前没有向上或向下舍入它们有某种关系。
OBP和SLG的列不正确,因为数学没有加起来,如果玩家的OBP或SLG为000,则自动使其为1.0,而我的OPS列将其变为1.0离开,我不知道如何纠正这个问题,或者我是否要将其变得复杂,而且我并不需要所有这些......这就是列应该用于数学的内容..
对于SLG,它是
总基数除以At-Bats。或(TB)/(AB)
对于OBP,它是
a)命中(H +步行(BB)+时间命中率(HBP)(此总和是基数的总时间)
(b)通过添加蝙蝠(AB),步行(BB)和时距命中(HBP)计算总板外观
(c)然后将总和(a)除以(b)(总板外观
提前谢谢