日期:2014-05-17  浏览次数:20569 次

google不到,求条SQL语句 带建表语句,求关注

create table TestGetMaxTime
(
name varchar(20),
time datetime,
c1 decimal(16,4),
c2 decimal(16,4),
)
insert into TestGetMaxTime values ('A','2013-01-30 01:00:00',12,2)
insert into TestGetMaxTime values('A','2013-01-30 02:00:00',12,4)
insert into TestGetMaxTime values('A','2013-01-30 03:00:00',12,3)
insert into TestGetMaxTime values('B','2013-01-30 01:00:00',12,6)
insert into TestGetMaxTime values('B','2013-01-30 02:00:00',12,1)
insert into TestGetMaxTime values('B','2013-01-30 03:00:00',12,2)

select * from TestGetMaxTime
drop table TestGetMaxTime


要求结果如下:
最大的
max(c1/c2)


name   time
A     2013-01-30 01:00:00
B     2013-01-30 02:00:00
sql

------解决方案--------------------
是否?

select name,time
from TestGetMaxTime  a
where not exists (
select 1 from TestGetMaxTime  b
where b.TestGetMaxTime  = a.TestGetMaxTime 
and c1/c2 > a.c1/a.c2
)

------解决方案--------------------
USE test
GO

create table TestGetMaxTime
(
    name varchar(20),
    time datetime,
    c1 decimal(16,4),
    c2 decimal(16,4),
)
insert into TestGetMaxTime values ('A','2013-01-30 01:00:00',12,2)
insert into TestGetMaxTime values('A','2013-01-30 02:00:00',12,4)
insert into TestGetMaxTime values('A','2013-01-30 03:00:00',12,3)
insert into TestGetMaxTime values('B','2013-01-30 01:00:00',12,6)
insert into TestGetMaxTime values('B','2013-01-30 02:00:00',12,1)
insert into TestGetMaxTime values('B','2013-01-30 03:00:00',12,2)
 
select 
name,time
from TestGetMaxTime AS a
WHERE NOT EXISTS(SELECT 1 FROM TestGetMaxTime AS x
WHERE x.name=a.name
AND x.c1/x.c2 > a.c1/a.c2
)
drop table TestGetMaxTime

/*
name  time                    
----- ----------------------- 
A     2013-01-30 01:00:00.000 
B     2013-01-30 02:00:00.000 
*/