code bbrq y gxrq xz gs ss sm id
000001 2008-10-31 00:00:00 1 2008-10-24 03:29:32.687 32B0 320828031.0000 NULL a NULL
000001 2009-06-22 00:00:00 1 2009-06-19 07:02:25.700 3110 2923674599.0000 NULL a NULL
000001 2009-06-22 00:00:00 1 2009-06-19 07:02:25.810 32B0 181759163.0000 NULL a NULL
000001 2009-06-22 00:00:00 1 2009-06-20 07:02:25.700 3110 29236.0000 NULL a NULL
000002 1991-01-29 00:00:00 1 2003-12-10 16:50:36.873 3110 17364700.0000 NULL a NULL
000002 1998-12-31 00:00:00 1 2003-12-10 16:52:03.997 3140 57090000.0000 NULL a NULL
以上表名是gzgb,我想取得此表中满足如下条件的记录:
1,code唯一
2,bbrq最新,如果bbrq相同取gxrq最新的
3,xz='3110' and y=1
得到的结果像如下样子:
code bbrq y gxrq xz gs ss sm id
000001 2009-06-22 00:00:00 1 2009-06-20 07:02:25.700 3110 29236.0000 NULL a NULL
000002 1991-01-29 00:00:00 1 2003-12-10 16:50:36.873 3110 17364700.0000 NULL a NULL
000001 2008-10-31 00:00:00 1 2008-10-24 03:29:32.687 32B0 320828031.0000 NULL a NULL
000001 2009-06-22 00:00:00 1 2009-06-19 07:02:25.700 3110 2923674599.0000 NULL a NULL
000001 2009-06-22 00:00:00 1 2009-06-19 07:02:25.810 32B0 181759163.0000 NULL a NULL
000001 2009-06-22 00:00:00 1 2009-06-20 07:02:25.700 3110 29236.0000 NULL a NULL
000002 1991-01-29 00:00:00 1 2003-12-10 16:50:36.873 3110 17364700.0000 NULL a NULL
000002 1998-12-31 00:00:00 1 2003-12-10 16:52:03.997 3140 57090000.0000 NULL a NULL
以上表名是gzgb,我想取得此表中满足如下条件的记录:
1,code唯一
2,bbrq最新,如果bbrq相同取gxrq最新的
3,xz='3110' and y=1
得到的结果像如下样子:
code bbrq y gxrq xz gs ss sm id
000001 2009-06-22 00:00:00 1 2009-06-20 07:02:25.700 3110 29236.0000 NULL a NULL
000002 1991-01-29 00:00:00 1 2003-12-10 16:50:36.873 3110 17364700.0000 NULL a NULL
where xz='3110' and y=1
and not exists (
select 1 from gzgb where xz='3110' and y=1
and code = a.code and (bbrq>a.bbrq or bbrq=a.bbrq and gxrq>a.gxrq)
)
where xz='3110' and y=1 and not exists(select * from tb where a.code=code and datediff(dd,a.bbrg,bbrq)<0)
and exists(select * from tb where a.code=code and a.bbrg=bbrq and datediff(dd,a.gxrq,gxrq)<0)
from gzgb a
where not exists(select 1 from gzgb where code = a.code and bbrq > a.bbrq and xz = '3110' and y = 1)
and xz = '3110' and y = 1
where xz='3110' and y=1 and not exists
(select * from tb where a.code=code and xz='3110' and y=1 and (bbrq=a.bbrq and datediff(dd,a.gxrq,gxrq)>0 or datediff(dd,a.bbrq,bbrq)>0 ))
Drop table #a
Create table #a(a int,b datetime,C int,D datetime,E nvarchar(20),f float)
insert into #a
select 1, '2008-10-31',1,'2008-10-24 03:29:32.687','32B0', 320828031.0000 union all
select 1,'2009-06-22', 1, '2009-06-19 07:02:25.700', '3110', 2923674599.0000 union all
select 1,'2009-06-22' , 1, '2009-06-19 07:02:25.810', '32B0', 181759163.0000 union all
select 1,'2009-06-22' , 1, '2009-06-20 07:02:25.700', '3110', 29236.0000 union all
select 2,'1991-01-29' , 1, '2003-12-10 16:50:36.873', '3110', 17364700.0000 union all
select 2,'1998-12-31' ,1 ,'2003-12-10 16:52:03.997', '3140' ,57090000.0000select * from #a inner join
(select a,max(b) as b,max(d) as d from #a
where e='3110' and c=1
Group by a) as #b
on #a.b=#b.b and #a.d=#b.d
结果
--------------
1 2009-06-22 00:00:00.000 1 2009-06-20 07:02:25.700 3110 29236 1 2009-06-22 00:00:00.000 2009-06-20 07:02:25.700
2 1991-01-29 00:00:00.000 1 2003-12-10 16:50:36.873 3110 17364700 2 1991-01-29 00:00:00.000 2003-12-10 16:50:36.873