日期:2014-05-17 浏览次数:20528 次
select t1.[name],t1.riqi,gongzi1,gongzi2
from 表1 t1
inner join 表2 t2 on t1.[name]=t2.[name] and t1.riqi=t2.riqi
--当然也可以把上述语句放到视图里
if OBJECT_ID('tempA', 'u') is not null drop table tempA;
go
create table tempA( [name] varchar(100), [riqi] varchar(100), [gongzi1] varchar(100));
insert tempA
select 'aaa','2013','50' union all
select 'bbb','2013','60' union all
select 'aaa','2014','55'
if OBJECT_ID('tempB', 'u') is not null drop table tempB;
go
create table tempB( [name] varchar(100), [riqi] varchar(100), [gongzi2] varchar(100));
insert tempB
select 'aaa','2013','40' union all
select 'aaa','2015','50'
--SQL:
CREATE VIEW v_test
AS
SELECT
name = COALESCE(a.name,b.name),
riqi = COALESCE(a.riqi,b.riqi),
gongzi1 = ISNULL(a.gongzi1,0),