Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/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 server 2008 如何在SQL中选择每列最大值?_Sql Server 2008_Loops_Group By_Aggregate Functions - Fatal编程技术网

Sql server 2008 如何在SQL中选择每列最大值?

Sql server 2008 如何在SQL中选择每列最大值?,sql-server-2008,loops,group-by,aggregate-functions,Sql Server 2008,Loops,Group By,Aggregate Functions,在SQL Server中,我有一个整数、日期时间和字符串的列表。比如说, number datetime string 6 2011-09-22 12:34:56 nameOne 6 2011-09-22 1:23:45 nameOne 6 2011-09-22 2:34:56 nameOne 5 2011-09-22 3:45:01 nameOne 5 2011-09-22 4:56:01

在SQL Server中,我有一个整数、日期时间和字符串的列表。比如说,

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
5       2011-09-22 3:45:01   nameOne
5       2011-09-22 4:56:01   nameOne
5       2011-09-22 5:01:23   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo
4       2011-09-21 3:45:01   nameTwo
4       2011-09-21 4:56:01   nameTwo
4       2011-09-21 5:01:23   nameTwo
declare @max int
declare my_cursor cursor fast_forward for
    select distinct string
    from table
open my_cursor
fetch next from my_cursor into @string
while @@fetch_status = 0
begin
    set @max = (select max(number) from table where string = @string)
    select * from table where number = @max
    fetch next from my_cursor into @string
end
close my_cursor
deallocate my_cursor
我想编写一个SQL语句,只输出每个字符串的最大行数。在这个例子中

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo
我知道我可以循环字符串列中的每个字符串,然后获取该字符串的最大值,然后选择与该最大值匹配的行。比如说,

number  datetime             string
6       2011-09-22 12:34:56  nameOne
6       2011-09-22 1:23:45   nameOne
6       2011-09-22 2:34:56   nameOne
5       2011-09-22 3:45:01   nameOne
5       2011-09-22 4:56:01   nameOne
5       2011-09-22 5:01:23   nameOne
7       2011-09-21 12:34:56  nameTwo
7       2011-09-21 1:23:45   nameTwo
7       2011-09-21 2:34:56   nameTwo
4       2011-09-21 3:45:01   nameTwo
4       2011-09-21 4:56:01   nameTwo
4       2011-09-21 5:01:23   nameTwo
declare @max int
declare my_cursor cursor fast_forward for
    select distinct string
    from table
open my_cursor
fetch next from my_cursor into @string
while @@fetch_status = 0
begin
    set @max = (select max(number) from table where string = @string)
    select * from table where number = @max
    fetch next from my_cursor into @string
end
close my_cursor
deallocate my_cursor
但是,我想知道是否有一种方法可以在不使用循环的情况下完成此任务(例如,通过使用聚合函数和分组)