看看下面的SQL怎么写,哪样最简单?

vanjayhsu 2008-12-16 02:53:17
现有如下两个表:
select * from pro_info
项目ID 项目开始日期 项目结束日期 下次付款日期 下次付款金额
pro_id pro_begin_time pro_end_time next_pay_day next_pay_money
A 20080101 20080601 20080207 100
A 20080101 20080601 20080307 200
A 20080101 20080601 20080607 300

select * from t

月份 项目ID 已收金额
200801 A
200802 A 100
200803 A 300
200804 A 300
200805 A 300
200806 A 600

希望通过上述两个表的关联,得到如下结果。请高手赐教。谢谢!
SQL?
月份 项目ID 已收金额 下次付款日期 下次付款金额
200801 A 20080207 100
200802 A 100 20080307 200
200803 A 300 20080607 300
200804 A 300 20080607 300
200805 A 300 20080607 300
200806 A 600
...全文
140 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
jinjazz 2008-12-16
  • 打赏
  • 举报
回复
1楼的有些问题,没有完全符合需要,改2楼的
jinjazz 2008-12-16
  • 打赏
  • 举报
回复
--建立测试环境
create table pro_info (pro_id varchar(20),pro_begin_time varchar(20),
pro_end_time varchar(20),next_pay_day varchar(20),next_pay_money int);
insert into pro_info select 'A','20080101','20080601','20080207','100' from dual;
insert into pro_info select 'A','20080101','20080601','20080307','200' from dual;
insert into pro_info select 'A','20080101','20080601','20080607','300' from dual;

create table t(月份 varchar(20),项目ID varchar(20),已收金额 int);
insert into t select '200801','A',null from dual;
insert into t select '200802','A','100' from dual;
insert into t select '200803','A','300' from dual;
insert into t select '200804','A','300' from dual;
insert into t select '200805','A','300' from dual;
insert into t select '200806','A','600' from dual;

--测试语句
select 月份,项目ID,已收金额,next_pay_day,next_pay_money from(
select t.*,a.* ,row_number() over(partition by 月份,项目ID order by next_pay_day) as rn
from t left join pro_info a on a.next_pay_day>t.月份 and a.next_pay_day not like t.月份||'%'
order by t.月份)b
where b.rn=1;

--删除测试环境
--drop table pro_info;
--drop table t;

/*--测试结果
月份 项目ID 已收金额 NEXT_PAY_DAY NEXT_PAY_MONEY
200801 A 20080207 100
200802 A 100 20080307 200
200803 A 300 20080607 300
200804 A 300 20080607 300
200805 A 300 20080607 300
200806 A 600
*/
jinjazz 2008-12-16
  • 打赏
  • 举报
回复
--建立测试环境
create table pro_info (pro_id varchar(20),pro_begin_time varchar(20),
pro_end_time varchar(20),next_pay_day varchar(20),next_pay_money int);
insert into pro_info select 'A','20080101','20080601','20080207','100' from dual;
insert into pro_info select 'A','20080101','20080601','20080307','200' from dual;
insert into pro_info select 'A','20080101','20080601','20080607','300' from dual;

create table t(月份 varchar(20),项目ID varchar(20),已收金额 int);
insert into t select '200801','A',null from dual;
insert into t select '200802','A','100' from dual;
insert into t select '200803','A','300' from dual;
insert into t select '200804','A','300' from dual;
insert into t select '200805','A','300' from dual;
insert into t select '200806','A','600' from dual;

--测试语句
select 月份,项目ID,已收金额,next_pay_day,next_pay_money from(
select t.*,a.* ,row_number() over(partition by 月份,项目ID order by next_pay_day) as rn
from t,pro_info a where a.next_pay_day>t.月份
order by t.月份)b
where b.rn=1;

--删除测试环境
--drop table pro_info;
--drop table t;

/*--测试结果
月份 项目ID 已收金额 NEXT_PAY_DAY NEXT_PAY_MONEY
200801 A 20080207 100
200802 A 100 20080207 100
200803 A 300 20080307 200
200804 A 300 20080607 300
200805 A 300 20080607 300
200806 A 600 20080607 300
*/

17,382

社区成员

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

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