在SQL中,如何查询某一字段中最大值的数据

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在SQL中,如何查询某一字段中最大值的数据相关的知识,希望对你有一定的参考价值。

使用类似如下语句找出
某个表中
某个字段
在你的某条件下,为最大的一条记录:
select max(字段) form 表格1 where 你的条件
--或用下面查看这条记录里所有的字段的情况
select * form 表格1 where 字段 in (select max(字段) form 表格1 where 你的条件)
至于
“及明细”,就不知道你是什么想法了,如果是所有记录,那就直接列出所有记录就好:
select * form 表格1 where 你的条件
参考技术A sql查询字段的最大值使用MAX()函数。
例:select
max(a)
from
table
语句大意:检索表table中a字段中的最大值。

sql中如何查看某一字段值有几个数值

用分组,组内计数就可以了,意思就是根据字段a的取值进行分组,相同的为一组,在用count进行组内计数select a,count(*)from Agroup by a 参考技术A CREATE proc Full_Search(@string varchar(50))
as
begin

declare @tbname varchar(50)
declare tbroy cursor for select name from sysobjects
where xtype= 'u ' --第一个游标遍历所有的表

open tbroy
fetch next from tbroy into @tbname
while @@fetch_status=0
begin

declare @colname varchar(50)
declare colroy cursor for select name from syscolumns
where id=object_id(@tbname) and xtype in (
select xtype from systypes
where name in ( 'varchar ', 'nvarchar ', 'char ', 'nchar ') --数据类型为字符型的字段
) --第二个游标是第一个游标的嵌套游标,遍历某个表的所有字段

open colroy
fetch next from colroy into @colname
while @@fetch_status=0
begin

declare @sql nvarchar(1000),@j int
select @sql= 'select @i=count(1) from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%'''
exec sp_executesql @sql,N'@i int output',@i=@j output --输出满足条件表的记录数
if @j> 0
BEGIN
select 包含字串的表名=@tbname
--exec( 'select distinct '+@colname+' from ' +@tbname + ' where '+ @colname+ ' like '+ '''%'+@string+ '%''')
END
fetch next from colroy into @colname
end

close colroy
deallocate colroy

fetch next from tbroy into @tbname
end
close tbroy
deallocate tbroy
end
go本回答被提问者采纳
参考技术B 很简单的事情,非得回答的这么复杂干嘛?我来回答一下。

SELECT count(DISTINCT col_name) from table_name;

以上是关于在SQL中,如何查询某一字段中最大值的数据的主要内容,如果未能解决你的问题,请参考以下文章

sql求某一字段中最大值和最小值的问题,高手请进!

hive如何按某一字段分组取最大值所在行的数据

hive如何按某一字段分组取最大值所在行的数据

如何使用sql函数平均值总数最小值最大值

mysql查询时间最大的某一列

SQL某一表中重复某一字段重复记录查询与处理