首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

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

2013-02-05 
google不到,求条SQL语句 带建表语句,求关注create table TestGetMaxTime(name varchar(20),time datetime,

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 
*/

热点排行