2015-12-08 53 views
0

嗨,我對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 

我敢肯定,我需要有一個最大或在我有條款的東西,但我所嘗試過的一切都沒有奏效。非常感謝這方面的幫助。

回答

2

您可以使用ROW_NUMBER

SELECT Questions_Id, Answer_Numeric, Option_Id, Description, [Count] 
FROM (
    SELECT ans.Questions_Id,ans.Answer_Numeric,ans.Option_Id, 
     opt.Description, count(ans.Option_Id) as [Count], 
     ROW_NUMBER() OVER (PARTITION BY ans.Questions_Id, ans.Answer_Numeric 
          ORDER BY count(ans.Option_Id) DESC) AS rn 
    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) AS t 
WHERE t.rn = 1 
ORDER BY 2, 5 desc 

或者您可以使用RANK以便處理關係,即每Questions_Id多個行,Answer_Numeric分區共享相同的最大Count數。

+0

有沒有一種方法可以獲得相對於Answer_Numeric總數的計數百分比? – mentos35

+0

@ mentos35您必須在單獨的派生表中計算總計數,然後將派生表與其餘表中的計算結合起來以執行劃分。 –

2

使用row_number()

SELECT * 
FROM (SELECT ans.Questions_Id, ans.Answer_Numeric, ans.Option_Id, opt.Description, 
      count(*) as cnt, 
      row_number() over (partition by ans.Questions_Id, ans.Answer_Numeric 
           order by count(*) desc) as seqnum 
     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 
    ) t 
WHERE seqnum = 1 
ORDER BY 2, 5 desc; 
0

我們可以得到相同的結果以不同的方式設置,我已經採取樣本數據集,你只是合併您加入這個代碼

declare @Table1 TABLE 
    (Id int, Answer int, OptionId int, Description varchar(14), Count int) 
; 

INSERT INTO @Table1 
    (Id, Answer, OptionId, Description, Count) 
VALUES 
    (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) 
; 

SELECT tt.Id, tt.Answer, tt.OptionId, tt.Description, tt.Count 
FROM @Table1 tt 
INNER JOIN 
    (SELECT OptionId, MAX(Count)OVER(PARTITION BY OptionId ORDER BY OptionId)AS RN 
    FROM @Table1 
    GROUP BY OptionId,count) groupedtt 
ON 
tt.Count = groupedtt.RN 
    WHERE tt.Count <> 5 
GROUP BY tt.Id, tt.Answer, tt.OptionId, tt.Description, tt.Count 

OR

select distinct Count, Description , Id , Answer from @Table1 e where 1 = 
(select count(distinct Count) from @Table1 where 
Count >= e.Count and (Description = e.Description)) 
相關問題