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
两表没有什么关系,就是需要查询相同天数的开支与供应商.不能有重复.如果B表数据多,A表数据记录少,那就用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
*/