有点难度的SQL语句

Tennal1020 2005-10-10 03:44:26
1. 表结构(TABLE1)
车牌号码,定位时间, 速度, 位置
123 2005-01-01 30 中国
123 2005-03-02 60 中国
456 2005-09-20 30 中国
456 2005-09-30 70 中国

2. 需求
每辆车在TABLE1表中可能存在多条记录,现在要求用一SQL语句查询出来各个车最早的那条定位数据。

3. 查询结果
车牌号码,定位时间, 速度, 位置
123 2005-01-01 30 中国
456 2005-09-20 30 中国

...全文
170 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZH_John 2005-10-11
  • 打赏
  • 举报
回复
select * from table1 where rowid in (select min(rowid) from table1 group by 车牌号码)

这条语句可以实现.
nowait 2005-10-10
  • 打赏
  • 举报
回复
select 车牌号码,定位时间, 速度, 位置
from
(
select 车牌号码,定位时间, 速度, 位置,
row_number()over(partition by 车牌号码 order by 定位时间 asc ) as rn from table1
)
where rn=1;
tangtangno1 2005-10-10
  • 打赏
  • 举报
回复
select 车牌号码,定位时间,
(select 速度 from table1 where 车牌号码=t1.车牌号码 and 定位时间=t1.定位时间 and rownumber=1) 速度,
(select 位置 from table1 where 车牌号码=t1.车牌号码 and 定位时间=t1.定位时间 and rownumber=1) 位置
from (select 车牌号码,min(定位时间) 定位时间 from table1 group by 车牌号码)t1
muyeliuxin 2005-10-10
  • 打赏
  • 举报
回复
不對,一看就知語法不通。
feng2 2005-10-10
  • 打赏
  • 举报
回复
SQL> select a1 "车牌号码",a5 "定位时间",a2 "速度",a3 "位置" from a;

车牌号码 定位时间 速度 位置
---------- ----------- ---------- ----------
123 2005-10-10 30 中国
123 2005-10-7 60 中国
456 2005-10-8 20 中国
456 2005-10-1 40 中国

Executed in 0.031 seconds

SQL> select a1 "车牌号码",a5 "定位时间",a2 "速度",a3 "位置" from
(select a1,a5,a2,a3,rank() over(partition by a1 order by a5) dk from a)
where dk = 1;

车牌号码 定位时间 速度 位置
---------- ----------- ---------- ----------
123 2005-10-7 60 中国
456 2005-10-1 40 中国

Executed in 0.015 seconds

SQL>
bobfang 2005-10-10
  • 打赏
  • 举报
回复
select a.* from (select b.*,rank() over(partition by 车牌号码 order by 定位时间) order1 from table1 b) a where a.order1=1;
lee_billiy 2005-10-10
  • 打赏
  • 举报
回复
waterfirer(水清) 的答案是正确的
waterfirer 2005-10-10
  • 打赏
  • 举报
回复
select * from table1 a where 定位时间=(select min(定位时间) from table1 where 车牌号码=a.车牌号码)
在 车牌号码 和 定位时间 都相同时才会取多条记录。
Tennal1020 2005-10-10
  • 打赏
  • 举报
回复
这样不行的,只要速度或位置不相同,则还是会每个车取多条记录。
sasacat 2005-10-10
  • 打赏
  • 举报
回复
啊,是我看错题目了.....掩面而奔....
sasacat 2005-10-10
  • 打赏
  • 举报
回复
是你想得太复杂了,还是我想得太简单?
waterfirer 2005-10-10
  • 打赏
  • 举报
回复
select * from table1 a where 定位时间=(select min(定位时间) from table1 where 车牌号码=a.车牌号码)
sasacat 2005-10-10
  • 打赏
  • 举报
回复
select 车牌号码,min(定位时间),速度,位置
from table1
group by 车牌号码,速度,位置

17,086

社区成员

发帖
与我相关
我的任务
社区描述
Oracle开发相关技术讨论
社区管理员
  • 开发
  • Lucifer三思而后行
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧