SQL中循环的基于集合的替代方法

时间:2016-04-13 10:56:48

标签: sql sql-server

我读过许多帖子,在SQL中你应该考虑集合,而不是循环。我正在尝试执行以下操作:

SELECT SUM(Score)
FROM (SELECT W.StemId, W.Weight*Q.Weight As Score
    FROM TfidfWeights AS W
    INNER JOIN Query AS Q ON W.StemId = Q.StemId
    WHERE W.DocumentId = '0054f88e-a565-4f54-866a-e6107948567e') TMP

如何获得'得分'对于每个' DocumentId'我有。换句话说,避免循环通过'文件'表并计算得分'对于每个文件。

1 个答案:

答案 0 :(得分:3)

获取每个DocumentID的总和

SELECT W.DocumentId, SUM(W.Weight*Q.Weight) As Score
FROM TfidfWeights AS W
INNER JOIN Query AS Q ON W.StemId = Q.StemId
GROUP BY W.DocumentId