当前位置: 代码迷 >> Oracle认证考试 >> 怎么從資料庫中抓取最新的客戶交易資料
  详细解决方案

怎么從資料庫中抓取最新的客戶交易資料

热度:6899   发布时间:2013-02-26 00:00:00.0
如何從資料庫中抓取最新的客戶交易資料
資料庫oracle
table名子aaa
欄位:
date_no :型態varchar2,存放的資料'YYYYMM'
name_no :型態varchar2

例如:
A客戶的最後一筆資料是在2009年八月進行交易的
B客戶的最後一筆資料是在2010年九月進行交易的....(有很多筆資料)

請問,要怎麼抓取客戶的最後一筆交易紀錄(也就是最新的一筆交易紀錄)

在oracle中的SQL要怎麼寫???

------解决方案--------------------------------------------------------
select * from (
select name_no, rownum() over (partition by date_no order by date_no desc) rn
from aaa
)
where rn = 1;
------解决方案--------------------------------------------------------
select * from (
select name_no, row_number() over (partition by name_no order by date_no desc) rn
from aaa
)
where rn = 1;
------解决方案--------------------------------------------------------
SQL code
select * from aaa t where date_no = (select max(date_no) from aaa where t.name_no=name_no);
  相关解决方案