求教SQL语句

Adebayor 2009-08-01 04:36:16
id name insertdate
1 a 2009-07-30
1 a 2009-07-31
1 b 2009-07-31
1 b 2009-08-01

如何得到下面结果:
id name insertdate
1 a 2009-07-31
1 b 2009-08-01
其中id,name,insertdate均为主键,
...全文
137 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
Adebayor 2009-08-08
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 inthirties 的回复:]
或者把你的子查询建一个view,就可以当作table一样用了,

不过你可以把你的子查询先贴上来,我们先看看是什么问题。导致的错误。
[/Quote]
恩 很好的建议
小灰狼W 2009-08-02
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 adebayor 的回复:]
引用 4 楼 fadingagile 的回复:
select *
from a
where (id,name,insertdate) in (
  select id,name,max(insertdate) insertdate
  from a
  group by id,name )


这么写肯定不可以了 好多列
如果不用rownum该怎么写呀?
[/Quote]

怎么不可以啊?6楼和9楼写法不同,但根本是一个效果啊
inthirties 2009-08-01
  • 打赏
  • 举报
回复
或者把你的子查询建一个view,就可以当作table一样用了,

不过你可以把你的子查询先贴上来,我们先看看是什么问题。导致的错误。
csuxp2008 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 adebayor 的回复:]
引用 10 楼 inthirties 的回复:
引用 9 楼 inthirties 的回复:
引用 8 楼 adebayor 的回复:
引用 7 楼 tiantom 的回复:
  引用 3 楼 inthirties 的回复:
  select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
  from a) where row_no = 1;

  使用分析函数比较容易实现

  不用分析函数该怎么搞?


如果不用只好自己group by了

select *
from a
where exist (select 1 from
  (select id,name,max(insertdate) insertdate
  from a
  group by id,name) b where a.id=b.id and a.name=b.name and a.insertdate=b.insertdate)


把exist改成exists

太感谢了 
不过还有个问题 如果a是张查询后得到的表 那该怎么写呀?
我这边一直报错 希望帮忙指点下
[/Quote]

很容易啊

select *
from a
这里的a用你的查询语句来代替,记得加个括号,再给个别名a就行了
inthirties 2009-08-01
  • 打赏
  • 举报
回复
把你的view贴出来看看
Adebayor 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 inthirties 的回复:]
引用 9 楼 inthirties 的回复:
引用 8 楼 adebayor 的回复:
引用 7 楼 tiantom 的回复:
  引用 3 楼 inthirties 的回复:
  select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
  from a) where row_no = 1;

  使用分析函数比较容易实现

  不用分析函数该怎么搞?


如果不用只好自己group by了

select *
from a
where exist (select 1 from
  (select id,name,max(insertdate) insertdate
  from a
  group by id,name) b where a.id=b.id and a.name=b.name and a.insertdate=b.insertdate)



把exist改成exists
[/Quote]
太感谢了
不过还有个问题 如果a是张查询后得到的表 那该怎么写呀?
我这边一直报错 希望帮忙指点下
inthirties 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 inthirties 的回复:]
引用 8 楼 adebayor 的回复:
引用 7 楼 tiantom 的回复:
引用 3 楼 inthirties 的回复:
select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
from a) where row_no = 1;

使用分析函数比较容易实现

不用分析函数该怎么搞?


如果不用只好自己group by了

select *
from a
where exist (select 1 from
  (select id,name,max(insertdate) insertdate
  from a
  group by id,name) b where a.id=b.id and a.name=b.name and a.insertdate=b.insertdate)

[/Quote]

把exist改成exists
inthirties 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 adebayor 的回复:]
引用 7 楼 tiantom 的回复:
引用 3 楼 inthirties 的回复:
select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
from a) where row_no = 1;

使用分析函数比较容易实现

不用分析函数该怎么搞?
[/Quote]

如果不用只好自己group by了

select *
from a
where exist (select 1 from
(select id,name,max(insertdate) insertdate
from a
group by id,name) b where a.id=b.id and a.name=b.name and a.insertdate=b.insertdate)
Adebayor 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 tiantom 的回复:]
引用 3 楼 inthirties 的回复:
select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
from a) where row_no = 1;

使用分析函数比较容易实现
[/Quote]
不用分析函数该怎么搞?
tiantom 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 inthirties 的回复:]
select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
from a) where row_no = 1;
[/Quote]
使用分析函数比较容易实现
Adebayor 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 fadingagile 的回复:]
select *
from a
where (id,name,insertdate) in (
  select id,name,max(insertdate) insertdate
  from a
  group by id,name )

[/Quote]
这么写肯定不可以了 好多列
如果不用rownum该怎么写呀?
csuxp2008 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 inthirties 的回复:]
select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
from a) where row_no = 1;
[/Quote]

UP
fadingagile 2009-08-01
  • 打赏
  • 举报
回复
select *
from a
where (id,name,insertdate) in (
select id,name,max(insertdate) insertdate
from a
group by id,name )
inthirties 2009-08-01
  • 打赏
  • 举报
回复
select * from (select a.*, row_number() over(partition by id, name order by insertdate desc) row_no
from a) where row_no = 1;
Adebayor 2009-08-01
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 hebo2005 的回复:]
好几种方法,说种最简单的
select id,name,max(insertdate) insertdate
from a
group by id,name
[/Quote]
忘了说了 里面还有好多字段 我是想先按id,name分组后 取出insertdate最大的那条数据
hebo2005 2009-08-01
  • 打赏
  • 举报
回复
好几种方法,说种最简单的
select id,name,max(insertdate) insertdate
from a
group by id,name

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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