2016-12-04 159 views
-1

我想從MS Access查詢棒球數據庫,但是我的SQL語句一直卡在缺少的運算符上。下面是我通過SQL查詢:在SQL查詢中缺少運算符

SELECT 
    Master.nameFirst, Master.nameLast, 
    Master.nameFirst + ' ' + Master.nameLast as FULLNAME, 
    (SUM(Batting.H) + SUM(Batting.BB) + SUM(Batting.HBP))/(SUM(Batting.AB) + SUM(Batting.BB) + SUM(Batting.HBP)) as OBP, 
    SUM(Batting.H)/(SUM(Batting.H) + SUM(Batting.BB) + SUM(Batting.HBP)) as HitP, 
    (SUM(Batting.2B) + SUM(Batting.3B) + SUM(Batting.HR))/SUM(Batting.H) as ExtraP, 
    SUM(Batting.HR)/(SUM(Batting.2B) + SUM(Batting.3B) + SUM(Batting.HR)) as HRP, 
    SUM(Batting.3B)/(SUM(Batting.2B) + SUM(Batting.3B)) as TriplePerc, 
    SUM(Batting.HBP)/(SUM(Batting.BB) + SUM(Batting.HBP)) as HBPP 
FROM 
    Master 
INNER JOIN 
    Batting ON Master.playerID = Batting.playerID 
WHERE 
    (Batting.playerID IN (SELECT playerID 
          FROM Master 
          WHERE (nameFirst = 'Ryan' AND nameLast = 'Howard')) 

丟失的操作錯誤似乎把重點放在這個單一的表達:

(SUM(Batting.2B) + SUM(Batting.3B) + SUM(Batting.HR))/SUM(Batting.H) as ExtraP 

但老實說,我看不出有什麼不妥的地方。我錯過了什麼?

+0

爲什麼不通過在條件解決之前刪除條件來簡化查詢?如果你仍然不解決問題,那就問一個問題。 –

+1

真的是你的專欄名稱嗎?這應該是問題。你正在使用哪個DBMS? –

+1

請添加 - (1)數據庫+版本的相關標籤(2)相關表格的DDL(想想支持您的容易程度) –

回答

0

而且,只是爲了讓一個建議,如果你使用的別名,並格式化你的SQL,你會發現你的代碼更容易閱讀,理解和調試:

SELECT m.nameFirst, m.nameLast, 
    m.nameFirst + ' ' + m.nameLast FULLNAME, 
    (SUM(b.H) + SUM(b.BB) + SUM(b.HBP))/
    (SUM(b.AB) + SUM(b.BB) + SUM(b.HBP)) OBP, 
    SUM(b.H)/(SUM(b.H) + SUM(b.BB) + SUM(b.HBP)) HitP, 
    (SUM(b.2B) + SUM(b.3B) + SUM(b.HR))/SUM(b.H) ExtraP, 
    SUM(b.HR)/(SUM(b.2B) + SUM(b.3B) + SUM(b.HR)) HRP, 
    SUM(b.3B)/(SUM(b.2B) + SUM(b.3B)) TriplePerc, 
    SUM(b.HBP)/(SUM(b.BB) + SUM(b.HBP)) HBPP 
FROM Master m JOIN Batting b 
    ON b.playerID = m.playerID 
    and b.playerId IN 
     (SELECT playerID FROM Master 
     WHERE(nameFirst = 'Ryan' 
      AND nameLast = 'Howard') 
+0

它是C#字符串的一部分。對窮人格式化抱歉! – jDave1984

+0

我認爲你錯過了所有'AS xxx'的計算?看看'SUM(b.3B))TriplePerc,' –

+0

這些「as」在SQL中是可選的。 –

0

這是四線,如:

SUM(Batting.H)/(SUM(Batting.H) + SUM(Batting.BB) + SUM(Batting.HBP)) as HitP, 

應該是:

SUM(Batting.H)/((SUM(Batting.H) + SUM(Batting.BB) + SUM(Batting.HBP)) as HitP, 

或:

SUM(Batting.H)/(SUM(Batting.H) + SUM(Batting.BB) + SUM(Batting.HBP) as HitP, 

編輯

噢,我的,那些都錯了。這是丟失的右括號 - 或者說省略掉第一個左括號:

WHERE 
    (Batting.playerID IN (SELECT playerID 
          FROM Master 
          WHERE (nameFirst = 'Ryan' AND nameLast = 'Howard')) 
+0

也許我已經爲足球做好準備了,但是你確定'應該'的語法嗎? ()對現在看起來不平衡? –

+0

嗯,我不知道計算的目的是什麼,所以第一個_將H與(H + BB + HBP)分開,而第二個_與H分開H,然後加上BB + HBP_。但最後一個沒有什麼意義,我現在看到了。所以「應該」似乎對我來說是正確的。 – Gustav

+1

我做老'保持parens計數,爲每個'加'1('和減去1')。希望以零結束。我得到一個額外的'('與'你應該'和'或':'我錯過了什麼?:) –

相關問題