嗨,我對SQL很新,所以如果這裏的解決方案很簡單,請耐心等待。在SQL中獲取特定組的Count()的最大值()
我的腳本
SELECT ans.Questions_Id,ans.Answer_Numeric,ans.Option_Id, opt.Description, count(ans.Option_Id) as [Count]
FROM Answers ans
LEFT OUTER JOIN Questions que
ON ans.Questions_Id = que.Id
LEFT OUTER JOIN Options opt
ON ans.Option_Id = opt.Id
WHERE que.Survey_Id = 1
and ans.Questions_Id = 1
GROUP By ans.Questions_Id,ans.Answer_Numeric,ans.Option_Id, opt.Description
ORDER BY 2, 5 desc
我試圖讓每個Answer_Numeric上面的數字響應(說明)。目前的結果是這樣的:
| Questions_Id | Answer_Numeric | Option_Id | Description | Count
-----------------------------------------------------------------------
| 1 | 1 | 27 | Technology | 183
| 1 | 1 | 24 | Personal Items | 1
| 1 | 2 | 28 | Wallet/Purse | 174
| 1 | 2 | 24 | Personal Items | 3
| 1 | 2 | 26 | Spiritual | 1
| 1 | 3 | 24 | Personal Items | 53
| 1 | 3 | 25 | Food/Fluids | 5
| 1 | 3 | 26 | Spiritual | 5
| 1 | 3 | 27 | Technology | 1
| 1 | 3 | 28 | Wallet/Purse | 1
從該例子從上面我需要它的數據是這樣的:
| Questions_Id | Answer_Numeric | Option_Id | Description | Count
-----------------------------------------------------------------------
| 1 | 1 | 27 | Technology | 183
| 1 | 2 | 28 | Wallet/Purse | 174
| 1 | 3 | 24 | Personal Items | 53
我敢肯定,我需要有一個最大或在我有條款的東西,但我所嘗試過的一切都沒有奏效。非常感謝這方面的幫助。
有沒有一種方法可以獲得相對於Answer_Numeric總數的計數百分比? – mentos35
@ mentos35您必須在單獨的派生表中計算總計數,然後將派生表與其餘表中的計算結合起來以執行劃分。 –