表一:两个字段 时间和用户
date1 user
03-21 zhu
03-22 adm
表二: 两个字段 时间和营业额
date2 amount
03-21 10000
03-26 20000
如何合并成:
date1 user amount
03-21 zhu 10000
03-22 adm 0
03-26 20000
2 个解决方案
#1
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([date1] nvarchar(25),[user] nvarchar(23))
Insert #T
select N'03-21',N'zhu' union all
select N'03-22',N'adm'
GO
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([date2] nvarchar(25),[amount] int)
Insert #T1
select N'03-21',10000 union all
select N'03-26',20000
Go
--测试数据结束
SELECT ( CASE WHEN date1 IS NOT NULL THEN date1
ELSE date2
END ) AS date1 ,
ISNULL(#T.[user],'') AS [user] ,
ISNULL(#T1.amount,0) AS [amount]
FROM #T
FULL JOIN #T1 ON date1 = date2
#2
谢谢!!!!
#1
--测试数据
if not object_id(N'Tempdb..#T') is null
drop table #T
Go
Create table #T([date1] nvarchar(25),[user] nvarchar(23))
Insert #T
select N'03-21',N'zhu' union all
select N'03-22',N'adm'
GO
if not object_id(N'Tempdb..#T1') is null
drop table #T1
Go
Create table #T1([date2] nvarchar(25),[amount] int)
Insert #T1
select N'03-21',10000 union all
select N'03-26',20000
Go
--测试数据结束
SELECT ( CASE WHEN date1 IS NOT NULL THEN date1
ELSE date2
END ) AS date1 ,
ISNULL(#T.[user],'') AS [user] ,
ISNULL(#T1.amount,0) AS [amount]
FROM #T
FULL JOIN #T1 ON date1 = date2
#2
谢谢!!!!