日期:2014-05-18  浏览次数:20387 次

这个SQL怎么写?按某个字段分组求最后面的值.
SQL code

F1                          F2       F3
2010-01-23 00:00:00    128525    3380.0
2010-01-23 00:00:00    128526    3381.0
2010-01-23 00:00:00    128529    3384.0
2010-01-25 00:00:00    128530    3383.0
2010-01-25 00:00:00    128531    3382.0
2010-01-25 00:00:00    128534    3379.0
2010-03-04 00:00:00    128535    3378.0
2010-03-04 00:00:00    128536    3377.0
2010-03-04 00:00:00    128539    3374.0
2010-03-25 00:00:00    128540    3373.0
2010-03-25 00:00:00    128541    3372.0
2010-03-25 00:00:00    128542    3371.0
2010-03-25 00:00:00    128565    3372.0
2010-03-25 00:00:00    128566    3373.0


是按F2排序的同时F1肯定也是排好的,可以看出F2是随F1渐渐变大的
需要求的结果如下:
SQL code

2010-01-23 00:00:00    128529    3384.0
2010-01-25 00:00:00    128534    3379.0
2010-03-04 00:00:00    128539    3374.0
2010-03-25 00:00:00    128566    3373.0



------解决方案--------------------
SQL code
select * 
from tb t 
where F2=(select max(F2) from tb where f1=t.f1)

------解决方案--------------------
SQL code
select f1, max(f2),max(f3)
from tab 
group by f1

------解决方案--------------------
SQL code
if object_id('[tb]') is not null drop table [tb]
go
create table [tb] (F1 datetime,F2 int,F3 numeric(5,1))
insert into [tb]
select '2010-01-23 00:00:00',128525,3380.0 union all
select '2010-01-23 00:00:00',128526,3381.0 union all
select '2010-01-23 00:00:00',128529,3384.0 union all
select '2010-01-25 00:00:00',128530,3383.0 union all
select '2010-01-25 00:00:00',128531,3382.0 union all
select '2010-01-25 00:00:00',128534,3379.0 union all
select '2010-03-04 00:00:00',128535,3378.0 union all
select '2010-03-04 00:00:00',128536,3377.0 union all
select '2010-03-04 00:00:00',128539,3374.0 union all
select '2010-03-25 00:00:00',128540,3373.0 union all
select '2010-03-25 00:00:00',128541,3372.0 union all
select '2010-03-25 00:00:00',128542,3371.0 union all
select '2010-03-25 00:00:00',128565,3372.0 union all
select '2010-03-25 00:00:00',128566,3373.0
select * 
from tb t 
where F2=(select max(F2) from tb where f1=t.f1)
/*
F1                      F2          F3
----------------------- ----------- ---------------------------------------
2010-03-25 00:00:00.000 128566      3373.0
2010-03-04 00:00:00.000 128539      3374.0
2010-01-25 00:00:00.000 128534      3379.0
2010-01-23 00:00:00.000 128529      3384.0

*/

------解决方案--------------------
SQL code
SELECT * FROM TB T WHERE F2=(SELECT MAX(F2) FROM TB WHERE DATEDIFF(DD,F1,T.F1)=0)

------解决方案--------------------
SQL code
select
 *
from
 tb t
where
 not exsits(select 1 from tb where  f1=t.f1 and F3>t.F3)

------解决方案--------------------
SQL code
--处理表重复记录(查询和删除)
/******************************************************************************************************************************************************
1、Num、Name相同的重复值记录,没有大小关系只保留一条
2、Name相同,ID有大小关系时,保留大或小其中一个记录
整理人:中国风(Roy)

日期:2008.06.06
******************************************************************************************************************************************************/

--1、用于查询重复处理记录(如果列没有大小关系时2000用生成自增列和临时表处理,SQL2005用row_number函数处理)

--> --> (Roy)生成測試數據
 
if not object_id('Tempdb..#T') is null
    drop table #T
Go
Create table #T([ID] int,[Name] nvarchar(1),[Memo] nvarchar(2))
Insert #T
select 1,N'A',N'A1' union all
select 2,N'A',N'A2' union all
select 3,N'A',N'A3' union all
select 4,N'B',N