现有如下两个表:
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 300select * 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
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 300select * 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
解决方案 »
- 关于同时更新同一条记录的问题
- v$db_cache_advice 字段解释
- 正方教务管理系统 数据库表结构问题
- oracle 怎样实现合并相同行 希望是 一条语句
- 求大牛解析下:数据差异原因
- 在Oracle 中创建存储过程时,要传递参数时要用数组,请问该怎么弄呢?
- Oracle存储过程问题:能不能把数据集当参数传到存储过程中?
- 一个困扰我很久的问题,今天终于忍受不了了
- 我为什么不能直接用Oracle中的Boolean数据类型,要如何才能使用?
- 高手进来一下!分不够再加
- oralce服务器系统崩溃问题?
- 数据库中的一些中文被显示成?, 大部分没有问题。数据库的字符集是 ZHS16CGB231280,请帮忙指导。谢谢
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
*/
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
*/
测试数据我是自动生成的,因为sqlserver和oracle的语法比较象,只是把insert后面改一下就能直接用了。http://topic.csdn.net/u/20081211/20/fbf981b8-0923-4393-899a-6b572061b184.html
结果不对,你是2月份的下次付款3月7号,我按照2月7号了。
一个月多次付款,就把最原始的明细按照月份先sum汇总一下,然后按照14楼语句的思路做查询
很简单的程序,把文本[code=BatchFile]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 [/code]转为sql语句
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;
我把以前delphi写的一个传到资源中,你 过会儿刷新看看http://jinjazz.download.csdn.net/
用select ... from t left join pro_info a on a.pro_id=t.pro_id