求sql

lingxuan0812 2011-06-23 01:55:00
test表如下:

id max_a max_date min_a min_a_date
1 10 2011/01/02 5 2011/01/04
1 5 2011/01/01 4 2011/01/05
1 10 2011/01/03 4 2011/01/03

2 10 2011/01/02 5 2011/01/04
2 5 2011/01/01 4 2011/01/05
2 10 2011/01/03 4 2011/01/03


请问一下,我该怎样取到同一个id的最大值以及最大值对应的时间,最小值以及最小值对应的时间呢?





...全文
146 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
lingxuan0812 2011-06-24
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 tangren 的回复:]
最大(小)值有多行,如何取??
[/Quote]

想要的效果如下:
id max_va max_va_date min_va min_va_date
1 10 2011/01/02 4 2011/01/05
2 10 2011/01/02 4 2011/01/05


求解?
hudingchen 2011-06-23
  • 打赏
  • 举报
回复
回答以下2楼的问题先
lingxuan0812 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 bobo12082119 的回复:]
引用 3 楼 lingxuan0812 的回复:
如果要和表sal表关联,就是说如果sal表中的id=test表中的id相等才求max_a,max_date,min_a,min_a_date
该如何写呢?

表之间的连接,你想要什么样的结果?
给出结果样式.....................
[/Quote]

效果:
id max_va max_va_date min_va min_va_date
1 10 2011/01/02 4 2011/01/05
2 10 2011/01/02 4 2011/01/05
Ade子夜 2011-06-23
  • 打赏
  • 举报
回复
row_number() over(PARTITION BY id ORDER BY time DESC)
304的的哥 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 lingxuan0812 的回复:]
如果要和表sal表关联,就是说如果sal表中的id=test表中的id相等才求max_a,max_date,min_a,min_a_date
该如何写呢?
[/Quote]
表之间的连接,你想要什么样的结果?
给出结果样式.....................
lingxuan0812 2011-06-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 zhuomingwang 的回复:]
SQL code

select id,max_a,max_date,min_a,min_a_date
from test t
where not exists(select 1 from test t1
where t.id=t1.id and t.max_a<t1.max_a and t.min_a>t1.min_a)
[/Quote]
如果要和表sal表关联,就是说如果sal表中的id=test表中的id相等才求max_a,max_date,min_a,min_a_date
该如何写呢?
sal表如下:
s_id
1
2
3
tangren 2011-06-23
  • 打赏
  • 举报
回复
最大(小)值有多行,如何取??
  • 打赏
  • 举报
回复
select id,max_a,max_date,min_a,min_a_date
from test t
where not exists(select 1 from test t1
where t.id=t1.id and t.max_a<t1.max_a and t.min_a>t1.min_a)

17,378

社区成员

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

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