求这个SQL怎么写?

zyzx1986 2009-07-28 09:44:48
表 T 数据如下:

BUSI_SID LOGTYPE SUBTYPE CURSTATUSTYPE
xt001 2 201 2
xt002 2 201 5
xt001 2 201 8


查询时 如果 busi_sid 相同的只取 CURSTATUSTYPE最大的记录。

想得到结果:

BUSI_SID LOGTYPE SUBTYPE CURSTATUSTYPE
xt002 2 201 5
xt001 2 201 8

这个语句怎么写好? 求助各位!!!




...全文
157 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
csuxp2008 2009-07-28
  • 打赏
  • 举报
回复
UP,用分析函数最简单了

[Quote=引用 9 楼 zxf_feng 的回复:]
用分析函数
select busi_sid,logtype,subtype,curstatustype
from
(select busi_sid,logtype,subtype,curstatustype,row_number() over(partition by busi_sid order by curstatustype desc) rn
from t) where rn=1
[/Quote]
cosio 2009-07-28
  • 打赏
  • 举报
回复
比较赞同9楼的写法!
inthirties 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 inthirties 的回复:]
select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE from (select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE, row_number() over(partition by BUSI_SID order by CURSTATUSTYPE desc) index_no from T) where index_no ¡­
[/Quote]

以上是10g的写法,支持分析函数

如果不支持的话

select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE from T where exists (select 1 from (select BUSI_SID, max(CURSTATUSTYPE) max_cnt from T group by BUSI_SID) b where b. BUSI_SID=T.BUSI_SID and b.max_cnt=T.CURSTATUSTYPE and rownum = 1);
阿三 2009-07-28
  • 打赏
  • 举报
回复
用分析函数
select busi_sid,logtype,subtype,curstatustype
from
(select busi_sid,logtype,subtype,curstatustype,row_number() over(partition by busi_sid order by curstatustype desc) rn
from t) where rn=1
inthirties 2009-07-28
  • 打赏
  • 举报
回复
select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE from (select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE, row_number() over(partition by BUSI_SID order by CURSTATUSTYPE desc) index_no from T) where index_no = 1;
cosio 2009-07-28
  • 打赏
  • 举报
回复
建议多贴些实例的数据!
zyzx1986 2009-07-28
  • 打赏
  • 举报
回复
5 楼的兄弟,这样写也得不到我想要的结果。
ks_reny 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用楼主 zyzx1986 的回复:]
表 T 数据如下:

BUSI_SID  LOGTYPE SUBTYPE CURSTATUSTYPE
xt001 2 201 2    
xt002 2 201 5    
xt001 2 201 8    


查询时 如果 busi_sid 相同的只取 CURSTATUSTYPE最大的记录。

想得到结果:

BUSI_SID  LOGTYPE SUBTYPE CURSTATUSTYPE    
xt002 2 201 5    
xt001 2 201 8

这个语句怎么写好? 求助各位!!!





[/Quote]

select * from T where not exists(select 1 from T  a where busi_sid=a.busi_sid and  CURSTATUSTYPE>a.CURSTATUSTYPE)
zyzx1986 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 cosio 的回复:]
SQL codeselectdistinct BUSI_SID,max(LOGTYPE),max(SUBTYPE),max(CURSTATUSTYPE)from 表groupby busi_sid
[/Quote]

这种查询出的结果不会把busi_sid 过滤掉哦。
quiettown 2009-07-28
  • 打赏
  • 举报
回复

select BUSI_SID, LOGTYPE, subtype, CURSTATUSTYPE from (
select BUSI_SID, LOGTYPE, subtype, CURSTATUSTYPE, row_number() over(partition by BUSI_SID order by CURSTATUSTYPE desc) rn
from t) tab where rn = 1;
majy 2009-07-28
  • 打赏
  • 举报
回复
select * from t, (
select busi_id, max(curstatustype) mt from t group by busi_sid) a
where t.busi_id = a.busi_id and t.curstatustype = a.mt
group by busi_sid, logtype, subtype, curstatustype --去掉可能存在同样是最大的crustatustype的数据
cosio 2009-07-28
  • 打赏
  • 举报
回复

select distinct BUSI_SID,max(LOGTYPE),max(SUBTYPE),max(CURSTATUSTYPE) from 表 group by busi_sid
ojuju10 2009-07-28
  • 打赏
  • 举报
回复


select * from t a
where not exists(select 1 from t b
where a.busi_id=b.busi_id and a.CURSTATUSTYPE<b.CURSTATUSTYPE)
syc840 2009-07-28
  • 打赏
  • 举报
回复
下面的SQL应该可以达到上述要求的效果:
SELECT * FROM tt
WHERE CURSTATUSTYPE IN (
SELECT MAX(CURSTATUSTYPE) FROM tt
group by BUSI_SID)
group by BUSI_SID,LOGTYPE,SUBTYP,CURSTATUSTYPE
阿三 2009-07-28
  • 打赏
  • 举报
回复
分析函数在8i,9i中也是可以用的(我已测试),并不是10g才可以的吧好像正则表达式是10g以后才支持的。
inthirties 2009-07-28
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 inthirties 的回复:]
select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE from (select BUSI_SID,LOGTYPE,SUBTYPE,CURSTATUSTYPE, row_number() over(partition by BUSI_SID order by CURSTATUSTYPE desc) index_no from T) where index_no ¡­
[/Quote]

分析函数需要数据库版本的支持,如果没有支持就只能用group by来做了。

17,377

社区成员

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

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