表A为计划收款表,表B为实际收款表。他们有一个关联字段BillID
表A:
计划日期 计划摘要 计划金额
BillID PlanDate PlanSummary PlanMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1461 2009-11-20 应付 7800
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000表B:
实际日期 实际摘要 实际金额
BillID PlaceDate PlaceSummary PlaceMoney
1461 2009-10-18 收款 90
1461 2009-11-20 收款 8000
最后生成视图
计划日期 计划摘要 计划金额 实际日期 实际摘要 实际金额
BillID PlanDate PlanSummary PlanMoney PlaceDate PlaceSummary PlaceMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1641 null null null 2009-10-18 收款 90
1461 2009-11-20 应付 7800 2009-11-20 收款 8000
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000由于2009-11-20计划与实球一样,所以为同行,若计划与实际不一样,则在计划自动插入一个空行,并且排序对应。
表A:
计划日期 计划摘要 计划金额
BillID PlanDate PlanSummary PlanMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1461 2009-11-20 应付 7800
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000表B:
实际日期 实际摘要 实际金额
BillID PlaceDate PlaceSummary PlaceMoney
1461 2009-10-18 收款 90
1461 2009-11-20 收款 8000
最后生成视图
计划日期 计划摘要 计划金额 实际日期 实际摘要 实际金额
BillID PlanDate PlanSummary PlanMoney PlaceDate PlaceSummary PlaceMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1641 null null null 2009-10-18 收款 90
1461 2009-11-20 应付 7800 2009-11-20 收款 8000
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000由于2009-11-20计划与实球一样,所以为同行,若计划与实际不一样,则在计划自动插入一个空行,并且排序对应。
表A为计划收款表,表B为实际收款表。他们有一个关联字段BillID
表A:
计划日期 计划摘要 计划金额
BillID PlanDate PlanSummary PlanMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1461 2009-11-20 应付 7800
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000表B:
实际日期 实际摘要 实际金额
BillID PlaceDate PlaceSummary PlaceMoney
1461 2009-10-18 收款 90
1461 2009-11-20 收款 8000
最后生成视图
计划日期 计划摘要 计划金额 实际日期 实际摘要 实际金额
BillID PlanDate PlanSummary PlanMoney PlaceDate PlaceSummary PlaceMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1641 null null null 2009-10-18 收款 90
1461 2009-11-20 应付 7800 2009-11-20 收款 8000
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000由于2009-11-20计划与实球一样,所以为同行,若计划与实际不一样,则在计划自动插入一个空行,并且排序对应。
drop table b;create table a(BillID int, PlanDate varchar(12),PlanSummary varchar(20), PlanMoney int);
insert into a
select
1461,'2009-10-02','应付',8000 union all select
1461,'2009-10-05','应付',9000 union all select
1461,'2009-11-20','应付',7800 union all select
1461,'2009-12-20','应付',6900 union all select
1461,'2010-01-20','应付',5000;create table b(BillID int, PlaceDate varchar(12),PlaceSummary varchar(20), PlaceMoney int);
insert into b
select
1461,'2009-10-18','收款',90 union all select
1461,'2009-11-20','收款',8000 --drop view a_b;
create view a_b as
select (case when a.BillID is null then b.BillID else a.BillID end) as BillID,
a.PlanDate '计划日期', a.PlanSummary '计划摘要', a.PlanMoney '计划金额',
b.PlaceDate '实际日期', b.PlaceSummary '实际摘要', b.PlaceMoney '实际金额'
from a full join b on a.BillID=b.BillID and a.PlanDate=b.PlaceDate;select * from a_b order by (case when 计划日期 is null then 实际日期 else 计划日期 end );
计划日期 计划摘要 计划金额 实际日期 实际摘要 实际金额
BillID PlanDate PlanSummary PlanMoney PlaceDate PlaceSummary PlaceMoney
1461 2009-10-02 应付 8000
1461 2009-10-05 应付 9000
1641 null null null 2009-10-18 收款 90 --这条数据从哪来的?,主要是billid
1461 2009-11-20 应付 7800 2009-11-20 收款 8000
1461 2009-12-20 应付 6900
1461 2010-01-20 应付 5000
if object_id('[A]') is not null drop table [A]
create table [A] (BillID int,PlanDate datetime,PlanSummary varchar(4),PlanMoney int)
insert into [A]
select 1461,'2009-10-02','应付',8000 union all
select 1461,'2009-10-05','应付',9000 union all
select 1461,'2009-11-20','应付',7800 union all
select 1461,'2009-12-20','应付',6900 union all
select 1461,'2010-01-20','应付',5000
--> 测试数据: [B]
if object_id('[B]') is not null drop table [B]
create table [B] (BillID int,PlaceDate datetime,PlaceSummary varchar(4),PlaceMoney int)
insert into [B]
select 1461,'2009-10-18','收款',90 union all
select 1461,'2009-11-20','收款',8000select isnull(a.billid,b.billid),A.plandate,a.plansummary,a.planmoney,B.PlaceDate,b.PlaceSummary,b.PlaceMoney from [A] full join [B]
on a.billid=b.billid
and a.PlanDate=b.PlaceDate
insert into [A]
select 1461,'2009-10-02','应付',8000 union all
select 1461,'2009-10-05','应付',9000 union all
select 1461,'2009-11-20','应付',7800 union all
select 1461,'2009-12-20','应付',6900 union all
select 1461,'2010-01-20','应付',5000
create table [B] (BillID int,PlaceDate datetime,PlaceSummary varchar(4),PlaceMoney int)
insert into [B]
select 1461,'2009-10-18','收款',90 union all
select 1461,'2009-11-20','收款',8000select isnull(a.BillID,b.BillID) BillID,
isnull(a.PlanDate,b.PlaceDate) Date,
a.PlanSummary , a.PlanMoney ,
b.PlaceSummary , b.PlaceMoney
from a full join b
on a.BillID = b.BillID and a.PlanDate = b.PlaceDate
order by BillID,Datedrop table a , b/*
BillID Date PlanSummary PlanMoney PlaceSummary PlaceMoney
----------- ------------------------------------------------------ ----------- ----------- ------------ -----------
1461 2009-10-02 00:00:00.000 应付 8000 NULL NULL
1461 2009-10-05 00:00:00.000 应付 9000 NULL NULL
1461 2009-10-18 00:00:00.000 NULL NULL 收款 90
1461 2009-11-20 00:00:00.000 应付 7800 收款 8000
1461 2009-12-20 00:00:00.000 应付 6900 NULL NULL
1461 2010-01-20 00:00:00.000 应付 5000 NULL NULL(所影响的行数为 6 行)*/