对于 Oracle 数据库,假设我这里有两个表(结构相似,但数据量更大)定义如下:
create table payments(
record_no INTEGER;
cust_no INTEGER;
amount NUMBER;
date_entered DATE;
);
insert into payments values(1,3,34.5,sysdate-1);
insert into payments values(2,2,34.5,sysdate-2);
insert into payments values(3,3,34.5,sysdate-18/1440);
insert into payments values(4,1,34.5,sysdate-1);
insert into payments values(5,2,34.5,sysdate-2/24);
insert into payments values(6,3,34.5,sysdate-56/1440);
insert into payments values(7,4,34.5,sysdate-2);
insert into payments values(8,2,34.5,sysdate-1);
create table customer(
cust_no INTEGER;
name VARCHAR2;
zip VARCHAR2;
);
insert into customer values(1,'Tom',90001);
insert into customer values(2,'Bob',90001);
insert into customer values(3,'Jack',90001);
insert into customer values(4,'Jay',90001);
现在我想生成一个包含这些列的报告(按付款日期获取每个客户订单的前两个付款金额和日期):
客户编号 | pay_amount1 | pay_date1 |pay_amount2 | pay_date2
我想要的样本报告
CUST_NO PAYMENT1 PAYDATE1 PAYMENT2 PAYDATE2
1 34.5 October, 09 2013 0 null
2 34.5 October, 08 2013 34.5 October, 09 2013
3 34.5 October, 09 2013 34.5 October, 10 2013
4 34.5 October, 08 2013 0 null
任何人都可以做出正确有效的查询吗?提前谢谢。