现有如下两个表:
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       

解决方案 »

  1.   

    哈哈。不会吧。我没看到。1楼可否告诉我他的标题,因为SQL语句怎么写的搜索结果实在太多。不好意思!2楼不会看不懂吧?我晕。
      

  2.   

    --建立测试环境
    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
    */
      

  3.   

    堪错一下--建立测试环境
    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
    */
      

  4.   

    牛人阿。我以为我sql写的快。看来天外有天人外有人阿,高人的思路令人赞叹!
      

  5.   


    测试数据我是自动生成的,因为sqlserver和oracle的语法比较象,只是把insert后面改一下就能直接用了。http://topic.csdn.net/u/20081211/20/fbf981b8-0923-4393-899a-6b572061b184.html
      

  6.   


    结果不对,你是2月份的下次付款3月7号,我按照2月7号了。
    一个月多次付款,就把最原始的明细按照月份先sum汇总一下,然后按照14楼语句的思路做查询
      

  7.   


    很简单的程序,把文本[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;
      

  8.   

    自己写的简陋小工具...你可以参考sql版狙狙和小楼的方法,写存储过程生成测试脚本。因为我对开发语言更熟悉,所以写了应用程序,总之实用就行了。
      

  9.   

    sql版块牛人真多。高人能否把你的工具给偶用用?偶对你佩服的五体投地阿
      

  10.   

    我在那个抢分的帖子中用的是c#写的工具,因为要.net framework支持,没有上传
    我把以前delphi写的一个传到资源中,你 过会儿刷新看看http://jinjazz.download.csdn.net/
      

  11.   

    you are all 高手!!
      

  12.   


    用select ... from t left join pro_info a on a.pro_id=t.pro_id