查表中按编号统计的最小值
表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',372insert into @tb select '001','2007-01-01','20:20',370insert into @tb select '001','2007-01-02','18:20',328insert into @tb select '001','2007-01-03','08:20',312insert into @tb select '002','2007-01-01','08:20',372insert into @tb select '002','2007-01-01','20:20',368insert into @tb select '002','2007-01-02','18:20',328insert into @tb select '002','2007-01-03','08:20',312insert into @tb select '003','2007-01-01','08:20',372insert into @tb select '003','2007-01-01','20:20',366insert into @tb select '003','2007-01-02','18:20',340insert into @tb select '003','2007-01-03','08:20',312select * from @tb a where not exists(select 1 from @tb where rno = a.rnoand 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