create table pay_item
(username varchar2(2),itemname varchar2(20),allprice int,buytime date);
insert into pay_item values('A','王老吉',5,to_date('2005/01/02 08:00:01','yyyy/mm/dd hh24:mi:ss'));
insert into pay_item values('A','可乐',3,to_date('2005/01/02 08:00:01','yyyy/mm/dd hh24:mi:ss'));
insert into pay_item values('B','雪碧',3,to_date('2005/01/02 08:00:02','yyyy/mm/dd hh24:mi:ss'));
insert into pay_item values('B','可乐',6,to_date('2005/01/02 08:00:02','yyyy/mm/dd hh24:mi:ss'));
insert into pay_item values('A','王老吉',5,to_date('2005/01/02 08:00:03','yyyy/mm/dd hh24:mi:ss'));
想得到的结果是
A 王老吉 5 1
A 可乐 3 1
B 雪碧 3 2
B 可乐 3 2
A 王老吉 5 3
就是加上一个订单号,相同用户的订单号相同,如果后面他又买东西了,算做另外一个订单,不知道排名函数怎么用~
------解决方案--------------------
- SQL code
select p.username,p.itemname,p.allprice,dense_rank() over ( order by p.buytime) rntfrom (select t.* ,rownum as rn from pay_item t) porder by p.rn
------解决方案--------------------
- SQL code
SQL> select t.*, dense_rank() over(order by buytime, username) 2 from pay_item t 3 / USERNAME ITEMNAME ALLPRICE BUYTIME DENSE_RANK()OVER(ORDERBYBUYTIM-------- -------------------- --------------------------------------- ----------- ------------------------------A 王老吉 5 2005/1/2 8: 1A 可乐 3 2005/1/2 8: 1C 美年达 3 2005/1/2 8: 2B 可乐 6 2005/1/2 8: 3B 雪碧 3 2005/1/2 8: 3A 王老吉 5 2005/1/2 8: 4