日期:2014-05-17 浏览次数:20493 次
CREATE TABLE #a
(
RiQi date,
aTest NVARCHAR(20)
)
CREATE TABLE #b
(
RiQi Date,
BTest NVARCHAR(20)
)
INSERT INTO #a( RiQi, aTest)VALUES('2013-06-11','开支-吃饭')
INSERT INTO #a( RiQi, aTest)VALUES('2013-06-11','开支-洗脚')
INSERT INTO #b( RiQi, BTest)VALUES('2013-06-11','供应商-联想')
--想要的结果
#a.RiQi #a.aTest #b.RiQi #b. BTest
-------------------------------------------
2013-06-11 开支-吃饭 2013-06-11 供应商-联想
2013-06-11 开支-洗脚 NULL NULL
USE tempdb
GO
CREATE TABLE #a
(
RiQi date,
aTest NVARCHAR(20)
)
CREATE TABLE #b
(
RiQi Date,
BTest NVARCHAR(20)
)
INSERT INTO #a( RiQi, aTest)VALUES('2013-06-11','开支-吃饭')
INSERT INTO #a( RiQi, aTest)VALUES('2013-06-11','开支-洗脚')
INSERT INTO #b( RiQi, BTest)VALUES('2013-06-11','供应商-联想')
SELECT
a.RiQi,a.aTest,b.RiQi,BTest
FROM
(SELECT *,Row=ROW_NUMBER()OVER(PARTITION BY RiQi ORDER BY RiQi) FROM #a) AS a
FULL JOIN
(SELECT *,Row=ROW_NUMBER()OVER(PARTITION BY RiQi ORDER BY RiQi) FROM #b) AS b ON a.RiQi=b.RiQi AND a.Row=b.Row
/*
RiQi aTest RiQi BTest
2013-06-11 开支-吃饭 2013-06-11 供应商-联想
2013-06-11 开支-洗脚 NULL NULL
*/