我有這樣一個數據表彙總:如何基於SQL中的幾個標準
QuestionID UserName UserWeightingForQuestion AnswerGivenForQuestion Metric
1 A 1.50 1 ToBeCalculated
1 B 1.00 2 ToBeCalculated
1 C 1.80 3 ToBeCalculated
1 D 1.20 1 ToBeCalculated
1 E 1.40 2 ToBeCalculated
2 A 1.20 2 ToBeCalculated
2 B 1.20 2 ToBeCalculated
2 C 1.10 4 ToBeCalculated
2 D 1.20 5 ToBeCalculated
...
對於每個問題分組,我想填充每個單元下面Metric
柱,以計算定義爲值如下所示:
Metric_For_User_A_For_QuestionID_X = SUM(Weights_With_The_Answer_Similar_To_What_Is_Given_By_User_A_In_QuestionID_Group = X)/DISTINCT(All_WEeights_In_One_QuestionID_Group = X)
具體地說,
Metric_For_User_A_For_QuestionID_1 = SUM(1.50+1.20)/(1.50+1.00+1.80+1.20+1.40)
Metric_For_User_B_For_QuestionID_1 = SUM(1.00+1.40)/(1.50+1.00+1.80+1.20+1.40)
Metric_For_User_C_For_QuestionID_1 = SUM(1.80)/(1.50+1.00+1.80+1.20+1.40)
Metric_For_User_D_For_QuestionID_1 = SUM(1.50+1.20)/(1.50+1.00+1.80+1.20+1.40)
Metric_For_User_E_For_QuestionID_1 = SUM(1.00+1.40)/(1.50+1.00+1.80+1.20+1.40)
對於QuestionID組= 2,我想重複上述過程。例如,
Metric_For_User_A_For_QuestionID_2 = SUM(1.20+1.20)/(1.20+1.10)
我是相當新的SQL,我相信OVER
或某種聚合功能可以被用來實現這一目標(?)如果這種計算可以在SQL,可能會有人用SQL專業知識爲我提供了一種實現我想要計算的方法。
原始表格有〜70m行,我正在使用SQL Server。非常感謝您的建議和答案!
你可以嘗試解釋這個邏輯嗎?對於我想要做的事情沒有任何意義。 –
@SeanLange我被朋友問到解決這個問題,所以我不確定他爲什麼要這樣做(我問他同樣的問題)。我試圖自己解決這個問題,但最終意識到,現在已經超越了我的SQL排版,以便高效地完成它。 – user1330974