2017-04-08 59 views
1

我已經嘗試了幾種不同的查詢,似乎無法找到一個沒有導致錯誤SQL查詢表更新值到另一個表,其中ID的比賽

附近有語法錯誤關鍵字的工作總結屬性''

我有一個名爲Scores的表格,它保存用戶ID,星期編號和測驗中的星期得分。

架構:

  • 分數(ID,用戶名,weekNumber,currentScore)

總共有12周,所以最後的每​​個用戶將具有在此表中12個條目

我有另一個表Leaderboard有架構:

  • 排行榜(Id,userName,week1,week2,week3 ..... week12,totalScore)

每個用戶在此表中只有一個條目。

我一直在試圖將scoreScore的總和從Score分別保存到Leaderboard的totalScore屬性中,但似乎無法找出正確的語法。

我的查詢如下:

UPDATE t1 
SET t1.totalScore = t2.completeScore 
FROM dbo.Leaderboard AS t1, 
    ((SELECT Id, SUM(weeklyScore) AS completeScore FROM dbo.Scores) as F 
     INNER JOIN 
      (SELECT Id FROM dbo.Scores GROUP BY Id) AS S ON F.Id = S.Id) AS t2 
WHERE t1.Id = t2.Id 

回答

0

試試這個,讓我們知道如何去:

var total1 = ("UPDATE t1 " + 
     "SET t1.totalScore = t2.completeScore " + 
     "FROM dbo.Leaderboard AS t1, (SELECT F.Id, F.completeScore FROM " + 
     "(SELECT Id, SUM(weeklyScore) AS completeScore FROM dbo.Scores GROUP BY ID) as F " + 
     "INNER JOIN (SELECT Id FROM dbo.Scores/* GROUP BY Id*/) AS S "+ 
     "ON F.Id = S.Id) AS t2 "+ 
     "WHERE t1.Id = t2.Id"); 

順便說一句,我認爲你是失蹤的定義中的GROUP BY條款F派生表,並且您不需要S派生表的定義中的GROUP BY

+0

是的,我明白你的意思是不是在S中需要GROUP BY子句,而是在F中。我試過這個查詢,並且收到的錯誤是「列'Id'被指定了多次t2'。 – Marguerite

+0

而不是'SELECT *'從F和S派生表中選擇你需要的列。 –

+0

yes that worked !! Thank你這麼多! – Marguerite

相關問題