group by 问题

shayebuhui0 2013-12-06 04:29:57
表结构
A1 A2
a 1
b 1
c 1
d 1
e 2
f 2
g 2
h 3
i 3

怎么能查询出这样的结果
a 1
e 2
h 3

...全文
162 8 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
西蒙海耶 2013-12-09
  • 打赏
  • 举报
回复
当cn=2时就相当于查询每组第二条数据
西蒙海耶 2013-12-09
  • 打赏
  • 举报
回复
楼主的意思很简单就是以列A2分组查询每组第一条记录
select A1,A2
  from (select t1.*, row_number() over(partition by A2 order by rownum) cn
          from t1)
 where cn = 1;
结果: A1 A2 -- ---------- a 1 e 2 h 3
大话EPM 2013-12-06
  • 打赏
  • 举报
回复
引用 5 楼 yangqiu0928 的回复:
[quote=引用 3 楼 NIan_jun 的回复:] [quote=引用 2 楼 yangqiu0928 的回复:] [quote=引用 1 楼 NIan_jun 的回复:] 不知道楼主在问什么
按照A2 group by分组,想要查询结果[/quote] 那按照你给的数据。 select min(A1),A2 from t group by A2 [/quote] 如果A1字段是随机生成的字符串呢,我只想取第一个[/quote] 5#的可以的
shayebuhui0 2013-12-06
  • 打赏
  • 举报
回复
引用 3 楼 NIan_jun 的回复:
[quote=引用 2 楼 yangqiu0928 的回复:] [quote=引用 1 楼 NIan_jun 的回复:] 不知道楼主在问什么
按照A2 group by分组,想要查询结果[/quote] 那按照你给的数据。 select min(A1),A2 from t group by A2 [/quote] 如果A1字段是随机生成的字符串呢,我只想取第一个
  • 打赏
  • 举报
回复

with test as (
select 'a' as a1,1 as a2 from dual
union all
select 'b',1 from dual
union all
select 'c',1 from dual
union all
select 'd',1 from dual
union all
select 'e',2 from dual
union all
select 'f',2 from dual
union all
select 'g',2 from dual
union all
select 'h',3 from dual
union all
select 'i',3 from dual
)
select a1,a2 from (
select a1,a2,row_number() over(partition by a2 order by a1) as rn from test
) where rn = 1;
NIan_jun 2013-12-06
  • 打赏
  • 举报
回复
引用 2 楼 yangqiu0928 的回复:
[quote=引用 1 楼 NIan_jun 的回复:] 不知道楼主在问什么
按照A2 group by分组,想要查询结果[/quote] 那按照你给的数据。 select min(A1),A2 from t group by A2
shayebuhui0 2013-12-06
  • 打赏
  • 举报
回复
引用 1 楼 NIan_jun 的回复:
不知道楼主在问什么
按照A2 group by分组,想要查询结果
NIan_jun 2013-12-06
  • 打赏
  • 举报
回复
不知道楼主在问什么

17,140

社区成员

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

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