接上贴,多表查询中重复的问题.
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
/*
RiQiaTestRiQiBTest
2013-06-11开支-吃饭2013-06-11供应商-联想
2013-06-11开支-洗脚NULLNULL
*/