sql求助,找出同一单位日期最大的一条记录,且单位只能出一次

xwt799023 2013-06-07 01:37:14
dwid data money
1 2013/5/26 23000.00
2 2013/5/3 23000.00
2 2013/5/1 22.00
3 2013/5/14 23000.00
3 2013/5/1 22.00
4 2013/5/1 30000.00

-----------------------------
要求出来的结果为

dwid data money
1 2013/5/26 23000.00
2 2013/5/3 23000.00
3 2013/5/14 23000.00
4 2013/5/1 30000.00
...全文
177 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
sych888 2013-06-08
  • 打赏
  • 举报
回复
select * from tab_name where (dwid,data) in (select dwid,max(data) from tab_name group by dwid);
cutebear2008 2013-06-07
  • 打赏
  • 举报
回复
select t1.* from table_name t1 where not exists (select 1 from table_name t2 where t1.dwid = t2.dwid and t2.date > t1.date);
善若止水 2013-06-07
  • 打赏
  • 举报
回复
一 子查询依据每个单位查出来最大的data 二 依据子查询的结果查出来所要的数据
NIan_jun 2013-06-07
  • 打赏
  • 举报
回复

with t as
(
select 1 dwid,to_date('2013/5/26','YYYY/MM/DD') date1,23000 money from dual
union all
select 2 dwid,to_date('2013/5/3','YYYY/MM/DD') m,23000 s from dual
union all
select 2 dwid,to_date('2013/5/3','YYYY/MM/DD') m,230000 s from dual
union all
select 2 dwid,to_date('2013/5/1','YYYY/MM/DD') m,22 s from dual
union all
select 3 dwid,to_date('2013/5/14','YYYY/MM/DD') m,23000 s from dual
union all
select 3 dwid,to_date('2013/5/1','YYYY/MM/DD') m,22 s from dual
union all
select 4 dwid,to_date('2013/5/1','YYYY/MM/DD') m,30000 s from dual
)
select dwid,date1,money from (
select dwid,date1,money,row_number()over(partition by dwid order by date1 desc,money desc) rn from t 
) where rn=1
如果存在相同日期取money 最大的记录
小海葵1 2013-06-07
  • 打赏
  • 举报
回复
select dwid, dd, money from (select row_number() over(partition by dd order by money desc) as rn, dwid, dd, money from test) where rn = 1
u010412956 2013-06-07
  • 打赏
  • 举报
回复
select dwid,data,money from (select t.* ,row_number() over(partition by dwid order by data desc) rn from t ) where rn=1

17,377

社区成员

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

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