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

查表中按编号统计的最小值
表A
rno rdate rtime ye
001 2007-01-01 18:20 372
001 2007-01-01 20:20 370
001 2007-01-02 18:20 328
001 2007-01-03 08:20 312
002 2007-01-01 08:20 372
002 2007-01-01 20:20 368
002 2007-01-02 18:20 328
002 2007-01-03 08:20 312
003 2007-01-01 08:20 372
003 2007-01-01 20:20 366
003 2007-01-02 18:20 340
003 2007-01-03 08:20 312
.....
统计表B
rno rdate rtime ye
001 2007-01-03 08:20 312
002 2007-01-03 08:20 312
003 2007-01-03 08:20 312
......

------解决方案--------------------
select *
from
 a t
 where ye=
(select min(ye) from a where rno=t.rno )

------解决方案--------------------
SQL code
select a.*
from a inner join (select rno,min(ye) from a group by rno) t on a.rno=t.rno

------解决方案--------------------
SQL code
declare @tb table (rno varchar(10),rdate varchar(10),rtime varchar(5),ye int)
insert into @tb select '001','2007-01-01','18:20',372
insert into @tb select '001','2007-01-01','20:20',370
insert into @tb select '001','2007-01-02','18:20',328
insert into @tb select '001','2007-01-03','08:20',312

insert into @tb select '002','2007-01-01','08:20',372
insert into @tb select '002','2007-01-01','20:20',368
insert into @tb select '002','2007-01-02','18:20',328
insert into @tb select '002','2007-01-03','08:20',312

insert into @tb select '003','2007-01-01','08:20',372
insert into @tb select '003','2007-01-01','20:20',366
insert into @tb select '003','2007-01-02','18:20',340
insert into @tb select '003','2007-01-03','08:20',312

select * from @tb a 
where not exists(
select 1 from @tb where rno = a.rno
and cast(rdate+' '+rtime as datetime)>cast(a.rdate+' '+a.rtime as datetime))

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

------解决方案--------------------
select t.* from tb where ye = (select max(ye) from tb where rno = t.rno) from tb t