将两个表合并table1:id   姓名      日期     支出
----------------------------
1    张三   2009/10/20  1000
2    张三   2009/10/21  1500
3    李四   2009/10/20  3000table2:id   姓名      日期     收入
----------------------------
1    张三   2009/10/25  200
2    李四   2009/10/25  400
3    李四   2009/10/27  800
3    李四   2009/10/27  900
须合并成表三:table3:id   姓名      日期       支出        日期      收入
-----------------------------------------------------
1    张三    2009/10/20   1000     2009/10/25   200
2    张三    2009/10/21   1500        null null3    李四    2009/10/20   3000     2009/10/25   400
4    李四       null      null     2009/10/27   800
5    李四       null      null     2009/10/27   900
要求有少许变态 :D,有谁提供解答?

解决方案 »

  1.   


    if object_id('tb1') is not null drop table tb1
    go 
    create table tb1(id int, 姓名 varchar(5),日期 datetime,支出 int)
    insert tb1 select
    1,    '张三',  '2009/10/20',  1000 union all select
    2,    '张三',  '2009/10/21',  1500 union all select
    3,    '李四',  '2009/10/20',  3000 if object_id('tb2') is not null drop table tb2
    go 
    create table tb2(id int, 姓名 varchar(5),日期 datetime,收入 int)
    insert tb2 select 
    1,    '张三' , '2009/10/25',  '200'  union all select
    2,    '李四' , '2009/10/25',  '400'  union all select
    3,    '李四' , '2009/10/27',  '800'  union all select
    3,    '李四' , '2009/10/27',  '900' 
    select *,px=identity(int,1,1) into #tb1 from tb1
    select *,px=identity(int,1,1) into #tb2 from tb2select  姓名=isnull(a.姓名,b.姓名),a.日期,支出,b.日期,收入
    from #tb1 a
    full join #tb2 b
    on a.px=b.px and a.姓名=b.姓名姓名    日期                      支出          日期                      收入
    ----- ----------------------- ----------- ----------------------- -----------
    张三    2009-10-20 00:00:00.000 1000        2009-10-25 00:00:00.000 200
    张三    2009-10-21 00:00:00.000 1500        NULL                    NULL
    李四    2009-10-20 00:00:00.000 3000        2009-10-27 00:00:00.000 800
    李四    NULL                    NULL        2009-10-25 00:00:00.000 400
    李四    NULL                    NULL        2009-10-27 00:00:00.000 900(5 行受影响)
    drop table #tb1
    drop table #tb2
      

  2.   

    create table tb1(id int, 姓名 varchar(5),日期 datetime,支出 int)
    insert tb1 select
    1,    '张三',  '2009/10/20',  1000 union all select
    2,    '张三',  '2009/10/21',  1500 union all select
    3,    '李四',  '2009/10/20',  3000 
    create table tb2(id int, 姓名 varchar(5),日期 datetime,收入 int)
    insert tb2 select 
    1,    '张三' , '2009/10/25',  '200'  union all select
    2,    '李四' , '2009/10/25',  '400'  union all select
    3,    '李四' , '2009/10/27',  '800'  union all select
    3,    '李四' , '2009/10/27',  '900'select isnull(m.姓名,n.姓名) 姓名 , m.日期 , m.支出 , n.日期 , n.收入 from
    (select * , px = (select count(1) from tb1 where 姓名 = t.姓名 and 日期 < t.日期) + 1 from tb1 t) m
    full join
    (select * , px = (select count(1) from tb2 where 姓名 = t.姓名 and 日期 < t.日期) + 1 from tb2 t) n
    on m.姓名 = n.姓名 and m.px = n.pxdrop table tb1 , tb2/*
    姓名    日期                                                     支出          日期                                                     收入          
    ----- ------------------------------------------------------ ----------- ------------------------------------------------------ ----------- 
    张三    2009-10-20 00:00:00.000                                1000        2009-10-25 00:00:00.000                                200
    张三    2009-10-21 00:00:00.000                                1500        NULL                                                   NULL
    李四    2009-10-20 00:00:00.000                                3000        2009-10-25 00:00:00.000                                400
    李四    NULL                                                   NULL        2009-10-27 00:00:00.000                                800
    李四    NULL                                                   NULL        2009-10-27 00:00:00.000                                900(所影响的行数为 5 行)
    */