3
SQL Server에서 정수, datetimes 및 문자열 목록이 있습니다. 예를 들어,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
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
각 문자열에 대해 최대 값을 갖는 행만 출력하는 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
은 내가 문자열 열의 각 문자열을 통해 루프 후, 다음, 해당 문자열의 최대를 얻을 최대 일치하는 행을 선택할 수 있다는 것을 알고있다. (예를 들어, 집계 함수 및 그룹화를 사용하여) 루프를 사용없이이 작업
을 수행 할 수있는 방법이 있는지 예를 들어, 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
그러나, 궁금입니다.