在SQL中获取特定Group的Count()的Max()

时间:2015-12-08 12:18:30

标签: sql-server

您好我对SQL很陌生,所以如果这里的解决方案很简单,请耐心等待。

我的剧本

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

我很确定我需要在我的Having子句中有一个max或者某些东西但是我尝试过的所有东西都没有用。真的很感激任何帮助。

3 个答案:

答案 0 :(得分: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个数字。

答案 1 :(得分: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;

答案 2 :(得分: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))