一个表footballplayer
Create Table footballplayer (
Name Varchar2(100),
dates Date,
numb Number(10)
);
name表示运动员的姓名
dates表示得分时间
numb表示得分数
Insert Into footballplayer Values('langhua',TO_DATE('2010-03-15 12:12:00','yyyy-mm-dd HH24:mi:ss'),30);
Insert Into footballplayer Values('langhua',TO_DATE('2010-03-15 13:12:00','yyyy-mm-dd HH24:mi:ss'),20);
Insert Into footballplayer Values('langhua2',TO_DATE('2010-03-15 08:12:00','yyyy-mm-dd HH24:mi:ss'),23);
Insert Into footballplayer Values('langhua2',TO_DATE('2010-03-15 12:12:00','yyyy-mm-dd HH24:mi:ss'),24);
Insert Into footballplayer Values('langhua3',TO_DATE('2010-03-15 16:12:00','yyyy-mm-dd HH24:mi:ss'),18);
Insert Into footballplayer Values('langhua3',TO_DATE('2010-03-15 17:12:00','yyyy-mm-dd HH24:mi:ss'),35);
Insert Into footballplayer Values('langhua3',TO_DATE('2010-03-15 18:12:00','yyyy-mm-dd HH24:mi:ss'),24);
现在要算出运动员最早得分超过20的时间和得分数(格式是运动员的名字,最早得分时间,得分数)
我是写出来了,但是大家谁一句就写的出来的SQL啊
[code="sql"]Select f.name,f.dates,max(f.numb) From footballplayer f Where (f.name,f.dates) In (
select t.Name,Min(t.dates) abc from footballplayer t
Where t.numb > 20
Group By t.Name
) Group By f.name,f.dates;[/code]
我的语句没问题呀,name,numb重复都对[code="java"]
select * from footballplayer a where (select count(1) from footballplayer b where a.name=b.name and a.dates=20 and b.numb>=20 ) group by a.name order by a.numb asc
[/code]
select a.name,a.numb,a.dates from footballplayer a,(select min(dates) dates,name from footballplayer where numb>20 group by name) b where a.name=b.name and a.dates=b.dates这样写就是正确的了。。。
首先确定得分超过20分的成绩里面时间最小的那一条,当然要按照名字分组,这个条件确定了,名字和成绩就能够固定了。。
我这个应该更简单吧...
[code="java"]SELECT t.name,MIN(t.dates) FROM footballplayer t Group By t.NAME Having SUM(t.numb)>=20[/code]
查出结果...
1 langhua 2010-3-15 12:12:00
2 langhua2 2010-3-15 8:12:00
3 langhua3 2010-3-15 16:12:00
select * from footballplayer a where (select count(1) from footballplayer b where a.name=b.name and a.dates=20 and b.numb>=20 ) group by a.name order by a.numb asc
你测试一下
[code="sql"]
select name,min(dates) from FOOTBALLPLAYER
where numb>=20
group by name
[/code]
上面的给错了
[code="sql"]
select a.*,b.NUMB from (
select name,min(dates) as dates from FOOTBALLPLAYER
where numb>=20
group by NAME
) a
join FOOTBALLPLAYER b
on a.dates=b.dates and a.name=b.name
[/code]
[code="sql"]select name,numb,dates from footballplayer where (dates,name) in (select min(dates) dates,name from footballplayer where numb>20 group by name);[/code]
这样更简单些,不知道效率怎样。
SELECT *
FROM (Select*
FROM footballplayer t Where T.NUMB>20 Order By T.DATES)
WHERE Rownum=1;
楼主!你写的语句有问题,因为表中没主键标识这种错误难免。如果有一个运动员有两次分数一样且都过了20分,你写的就不成立了,比如,你把运动员3第一时间的成绩也改为35,你写的运行结果就错了,建议建表时要用主键,不好意思,因为没主键,我写不对,对不起了,呵呵
to wenchaoeye 我是直接拷贝它的表结构,关于什么主键,没有提到过,也没有多想。我也试过插入重复得语句。结果没有错误。我不太理解。
这个一样了吧。你可以测试一下。
Create Table footballplayer (
Name Varchar(100),
dates Date,
numb int(10)
);
Insert Into footballplayer Values('langhua','2010-03-15 12:12:00',30);
Insert Into footballplayer Values('langhua','2010-03-15 13:12:00',20);
Insert Into footballplayer Values('langhua2','2010-03-15 08:12:00',23);
Insert Into footballplayer Values('langhua2','2010-03-15 08:12:00',23);
Insert Into footballplayer Values('langhua2','2010-03-15 12:12:00',24);
Insert Into footballplayer Values('langhua3','2010-03-15 16:12:00',18);
Insert Into footballplayer Values('langhua3','2010-03-15 17:13:00',33);
Insert Into footballplayer Values('langhua3','2010-03-15 17:12:00',35);
Insert Into footballplayer Values('langhua3','2010-03-15 17:12:00',35);
Insert Into footballplayer Values('langhua3','2010-03-15 17:12:00',35);
Insert Into footballplayer Values('langhua3','2010-03-15 18:12:00',24);
这个Sql不可能存在有重复的问题,毫无疑问,Lz想要的就是时间最早的,超过20分的那条记录,首先,时间最早,条件超过20分那么,这两个条件确定的就是一条数据了,一个“最”字在这里放着呢?然后,根据名字和时间相等的取出来,不可能重复的。。。
........... where numb>=20 order by date
........... where numb>=20 and rownum=1 order by date
dates是时间类型,很精确,一般不会重复,至少name和dates一起不会重复
select * from footballplayer where numb>20 group by Names having dates=min(dates)
直接
select name,numb,Min(dates) abc from footballplayer
Where numb > 20
Group By name
不用numb分组
select * from footballplayer where numb>20 group by Names having dates=min(dates)
Names,dates,numb
'langhua',2010-03-15 12:12:00.0,30.0
'langhua2',2010-03-15 08:12:00.0,23.0
'langhua3',2010-03-15 17:12:00.0,35.0
it0604 分组没学好,你那个不能取numb
CSTS 直接写 min 估计也不行,好久没写sql,现在没个现成的数据库,有我就试下了
我用的是sybase 数据库试的,sql server我也不清楚,应该能行。sybase里面name 是关键字所以我改成了names.
我又看了下分组的语法,下面这个对,推荐
select * from footballplayer where numb>20 group by Names having dates=min(dates)
但还是无法确定地消除可能的时间重复记录
从实际意义看,同一个运动员不可能有时间重复的得分记录
仅看题意,要算出运动员最早得分超过20的时间和得分数,如果同一个运动员得分超过20最早的时间重复了,那么它们都是题意要求的记录
select * from footballplayer where dates=(select min (dates)
from footballplayer where name in(select name from footballplayer where numb>20))
select top(1)* from footballplayer where numb>20 order by dates
这个最好呵呵!~!~
liangzhongzhao126 你看清楚要求阿, top 1 只可能出一条的,就是大于20。时间最小的那条,跟人没关了
[quote="zhanghaocool"]
it0604 分组没学好,你那个不能取numb
CSTS 直接写 min 估计也不行,好久没写sql,现在没个现成的数据库,有我就试下了
[/quote]
我是Mysql5.2的语法,Oracle可以用having。你估计个毛啊,不知道不要乱下判断
我用的Sybase数据库,sql 应该是一样的阿。难道Oracle里where 跟having不能一起用?
Create Table footballplayer (
Names Varchar(100),
dates datetime,
numb float
)
drop table footballplayer
Insert Into footballplayer Values('langhua',CONVERT(datetime,'2010-03-15 12:12:00'),30)
Insert Into footballplayer Values('langhua',CONVERT(datetime,'2010-03-15 13:12:00'),20)
Insert Into footballplayer Values('langhua2',CONVERT(datetime,'2010-03-15 08:12:00'),23)
Insert Into footballplayer Values('langhua2',CONVERT(datetime,'2010-03-15 12:12:00'),24)
Insert Into footballplayer Values('langhua3',CONVERT(datetime,'2010-03-15 16:12:00'),18)
Insert Into footballplayer Values('langhua3',CONVERT(datetime,'2010-03-15 17:12:00'),35)
Insert Into footballplayer Values('langhua3',CONVERT(datetime,'2010-03-15 18:12:00'),24)
select * from footballplayer where numb>20 group by Names having dates=min(dates)
应该是having里不能用dates=min(dates)
select * from footballplayer where dates=(select min(dates) from footballplayer where numb>20 group by dates rownum=1 )
一句好像写不出来,和你写的好像有点像!
奇怪 试下这个
select * from ( select Names, dates from footballplayer where numb>20) g group by g.Names having g.dates=min(g.dates)
用PLSQL吧
select fp.* from footballplayer fp where fp.numb>20 and fp.dates=(select min(dates) from footballplayer where numb>20)
你有的什么数据库啊,可以用 [color=red]TOP[/color] 啊,
我有的MySql,MySql 没有Top 关键字,用 LIMIT 代替
语句如下 :
[color=red]select Name,Dates,numb from test.footballplayer
where
numb > 20
order by
Dates[/color]
LIMIT 1
如果其它的数据库可以更改为:
[color=red]select Top 1 Name,Dates,numb from test.footballplayer
where
numb > 20
order by
Dates
[/color]
查询结果如下:
Id Name Dates numb
"langhua2","2010-03-15 08:12:00", 23
希望是你想要的结果! :)
[code="java"]select A.playerName,A.score,A.date
from player A
where
A.score > 20
and not exists (
select 1 from player
where score=A.score
and date and playernameA.playername
)
select A.playerName,A.score,A.date
from player A
where
score > 20
and date in(
select min(date)
from player
group by score
) [code="java"][/code][/code]
呵呵,我是说在不同的时间得一样的分数,我又试了一下,你的语句是对的,其实你已经在意识中就将时刻作为唯一标识在用了,这跟主键的意义差不多了,呵呵
select f.name, min(f.dates) dates, f.numb
from footballplayer f
where numb > 20
group by f.name;
select name,Dates,namb from footballplayer
where
numb > 20
group by Name
order by
Dates asc
这样是不是跟简单点呢! :)
select * from
(select name,numb,dates abc from footballplayer
Where numb > =20
order by dates)
where rownum=1;
这样就可以啦,不知道有人回答出来没 ,我是没看前面的
oracle
[code="sql"]select name,dates,t.numb from footballplayer t where t.numb>20 and rownum<2 order by dates
[/code]
首先lz的题有歧义。numb得分数有两种解释,一种是到dates这个时刻时球员的总得分,另一种是在dates这个时刻所获得的得分。我想lz的意思是后者。但是怎么来解释lz的sql都有一些问题吧?如果是第一种情况,这个问题就很简单了。就是一个条件加排序然后取第一条记录。但是要是是第二种情况,你需要的信息就不是你说的这么容易得到。一条sql是写不出来的,即使用存储过程来写,也会不叫复杂,我目前想到的算法都是很冗余的(我想到最好的办法是二分法),这种情况是在数据设计规范中不被允许的。因此,这是数据表结构设计的问题,不是写sql的问题。应该在表结构中记录每个时间点总得分数。
select * from(select name,numb,dates from footballplayer
where numb>=20 order by date)rownum=1
这个满足要求吗
select * from footballplayer
langhua 2010-03-15 12:12:00.000 30
langhua 2010-03-15 13:12:00.000 20
langhua2 2010-03-15 08:12:00.000 23
langhua2 2010-03-15 12:12:00.000 24
langhua3 2010-03-15 16:12:00.000 18
langhua3 2010-03-15 17:12:00.000 35
langhua3 2010-03-15 18:12:00.000 24
select t1.* from footballplayer t1 where t1.numb>20 and exists (select * from footballplayer t2 where t2.name = t1.name and t1.dates < t2.dates )
langhua 2010-03-15 12:12:00.000 30
langhua2 2010-03-15 08:12:00.000 23
langhua3 2010-03-15 17:12:00.000 35
如果有主键的话,sqlserver就是这样的
select * from a where a.id in(select top 1 aa.ID from a aa where aa.name=a.name and aa.num>20 order by date asc )
oracle的话需要稍微改造一下就可以了,大概就是这样的吧(没环境你自己测):
select * from a where a.id in(select aa.ID from a aa where aa.name=a.name and aa.num>20 and aa.rownum=1 order by aa.date asc )
[code="sql"]asdfasfad[/code]
你用的是ORACLE数据库? 那样即使没有主键也没关系
查询的时候 RowNum也一起查出来做条件
select rownum,Name,dates,numb from footballplayer where numb>20
and rownum = 1(数字一)
select * from footballplayer f where f.dates=(select min(t.dates) from footballplayer t where t.numb>=20 )
大家可以试试这句:
select * from footballplayer a where a.numb>20 and a.dates=(SELECT min(dates) FROM footballplayer where numb>20)
select * from footballplayer where dates=(select min(d) from (select dates d,f.* from footballplayer f where numb>20) s )
select * from footballplayer where dates=(select min(d) from (select dates d,f.* from footballplayer f where numb>20) s )
SELECT t.name 姓名,MIN(t.dates) 得分时间,t.numb 分数 FROM footballplayer t Group By t.NAME,t.numb Having SUM(t.numb)>=20
这样能跑起来。
装了个10g,没有找到楼主说的10i,只好用10g对付了。
select name,first_value(date) over(order by numb) ,numb where numb >=20
上次错了。时间没排序 不过那个别人回答了,不过想了个通用的方法:
select name,dates,numb from footballplayer f where(not exists(select 1 from footballplayer f2 where f2.dates > f1.dates and f2.numb <20))
用的是公司的数据库 不敢搞建删表 没测试过 试试 应该可以了
可以包含相同的记录 不想有重复就distinct掉吧
对了。在加个索引 效率会更好哦!
select name,min(dates) from FOOTBALLPLAYER
where numb>=20
group by name 这是SQL2000的 ,我的想法是:取 (dates)的时候不要当字段取,通过运算当定值取 就可以group by了。
[color=darkred][img][/img][img] 8) [/img][img][/img][/color]