Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何选择具有最大值的行?_Sql_Sqlite - Fatal编程技术网

Sql 如何选择具有最大值的行?

Sql 如何选择具有最大值的行?,sql,sqlite,Sql,Sqlite,给定此表,我希望为每个不同的url检索具有最大计数的行。此表的输出应为:“dell.html”3、“lenovo.html”4、“toshiba.html”5 +----------------+-------+ | url | count | +----------------+-------+ | 'dell.html' | 1 | | 'dell.html' | 2 | | 'dell.html' | 3 | | 'lenov

给定此表,我希望为每个不同的url检索具有最大计数的行。此表的输出应为:“dell.html”3、“lenovo.html”4、“toshiba.html”5

+----------------+-------+
| url            | count |
+----------------+-------+
| 'dell.html'    |     1 |
| 'dell.html'    |     2 |
| 'dell.html'    |     3 |
| 'lenovo.html'  |     1 |
| 'lenovo.html'  |     2 |
| 'lenovo.html'  |     3 |
| 'lenovo.html'  |     4 |
| 'toshiba.html' |     1 |
| 'toshiba.html' |     2 |
| 'toshiba.html' |     3 |
| 'toshiba.html' |     4 |
| 'toshiba.html' |     5 |
+----------------+-------+
要执行此操作,我需要编写什么SQL查询?

尝试使用此查询:

select url, max(count) as count
from table_name
group by url;
尝试使用此查询:

select url, max(count) as count
from table_name
group by url;
使用聚合函数

  select max(count) ,url from table_name group by url
从您的评论来看,您似乎需要相关的子查询

select t1.* from table_name t1
 where t1.count = (select max(count) from table_name t2 where t2.url=t1.url
                 )
如果您的sqllite版本支持行号 然后您可以像下面这样编写查询

select * from 
(
select *,row_number() over(partition by url  order by count desc) rn
  from table_name
) a where a.rn=1
使用聚合函数

  select max(count) ,url from table_name group by url
从您的评论来看,您似乎需要相关的子查询

select t1.* from table_name t1
 where t1.count = (select max(count) from table_name t2 where t2.url=t1.url
                 )
如果您的sqllite版本支持行号 然后您可以像下面这样编写查询

select * from 
(
select *,row_number() over(partition by url  order by count desc) rn
  from table_name
) a where a.rn=1

注释的可能重复项:
ROW\u NUMBER
可能是此处的一个选项,具体取决于您的SQLite版本。注释的可能重复项:
ROW\u NUMBER
可能是此处的一个选项,具体取决于您的SQLite版本。我的表实际上有更多的列,我正在执行与您的
select*,max(count)类似的查询从表\u按url命名组
。但这会输出两个计数字段。有没有办法让它只输出表中的所有列,还是忽略输出中的额外列就可以了?@elk是的,有办法,但你必须共享表中的详细信息,因为根据你的问题,这是答案,在我的问题中应该更清楚,但相关子查询是我要找的。我的表实际上有更多的列,我在做一个类似于您的查询
select*,max(count)from table\u name group by url
。但这会输出两个计数字段。有没有办法让它只输出表中的所有列,还是忽略输出中的额外列就可以了?@elk是的,有办法,但你必须共享表中的详细信息,因为根据你的问题,这是答案,在我的问题中应该更清楚,但相关子查询是我要找的。