首页 > 解决方案 > 过滤多数数

问题描述

目前,我的查询输出产生了多个带有 user_name 列的用户。我想过滤,只显示组中列出的多数用户(前 1 名)。

在此处输入图像描述

在此处输入图像描述

标签: reporting-servicessql-server-2008-r2

解决方案


我试图从 sql 的角度来满足您的要求。我们如何限制来自数据库的数据,因为只有那些记录,例如joe user 2数据才应该出现。而不是在 ssrs 方面工作,即在加载完整数据之后将是一个痛苦的过程。我为 sql 创建了一个小提琴。您能否看一下它,以便您了解如何限制数据。

这是小提琴链接:http ://sqlfiddle.com/#!9/8f78c5/2

下面的代码:架构

-- borrowed from https://stackoverflow.com/q/7745609/808921

CREATE TABLE IF NOT EXISTS `docs` (
  `id` int(6) unsigned NOT NULL,
  `rev` int(3) unsigned NOT NULL,
  `content` varchar(200) NOT NULL,
  PRIMARY KEY (`id`,`rev`)
) DEFAULT CHARSET=utf8;
INSERT INTO `docs` (`id`, `rev`, `content`) VALUES
  ('1', '1', 'The earth is flat'),
  ('2', '1', 'One hundred angels can dance on the head of a pin'),
  ('1', '2', 'The earth is flat and rests on a bull\'s horn'),
  ('1', '3', 'The earth is like a ball.'),
   ('2', '2', 'One hundred angels can dance on the head of a pin');

下面的 SQL 查询

select * from  `docs` where id=(select id from `docs` group by id order by count(id) desc LIMIT 1)

推荐阅读