使用完全外部联接加入两个临时 Table
Join Two Temp Table With Full Outer Join
我想用完全外连接连接两个临时表,但不能正常工作
总是只显示#RMS 值而不显示#RMB !!
这段代码哪里出了问题?
(#RMS 没有空值)
create table #RMS
(
[Year] int,
[Month] int,
sTAccount bigint,
sRemaining bigint
)
insert into #RMS(Year,Month,sTAccount,sRemaining)
select
YEAR(Date) [Year],
DATEPART(MONTH,Date) [Month],
sum(TAccount) sTAccount,
sum(Remaining) sRemaining
from
SaleInvoices
group by YEAR(Date),DATEPART(MONTH,Date)
order by YEAR(Date),DATEPART(MONTH,Date)
( #RMB without null but sometimes #RMB Month Column Value and #RMS Month Column value is Different )
create table #RMB
(
[Year] int,
[Month] int,
bTAccount bigint,
bRemaining bigint
)
insert into #RMB(Year,Month,bTAccount,bRemaining)
select
YEAR(Date) [Year],
DATEPART(MONTH,Date) [Month],
sum(TAccount) bTAccount,
sum(Remaining) bRemaining
from
BuyInvoices
group by YEAR(Date),DATEPART(MONTH,Date)
order by YEAR(Date),DATEPART(MONTH,Date)
select * from #RMS
Full Outer Join #RMB
on #RMS.Year=#RMB.Year and #RMS.Month=#RMB.Month
group by #RMS.Year, #RMS.Month
order by #RMS.Year, #RMS.Month
感谢您的回答
您的 SELECT
列表有误。将 *
替换为 #RMS.*, #RMB.*
或(更好)您想要的字段的明确列表,在它们的名称前加上它们来自的 table 的名称。这也允许不重复您加入的字段。
我想用完全外连接连接两个临时表,但不能正常工作 总是只显示#RMS 值而不显示#RMB !!
这段代码哪里出了问题?
(#RMS 没有空值)
create table #RMS
(
[Year] int,
[Month] int,
sTAccount bigint,
sRemaining bigint
)
insert into #RMS(Year,Month,sTAccount,sRemaining)
select
YEAR(Date) [Year],
DATEPART(MONTH,Date) [Month],
sum(TAccount) sTAccount,
sum(Remaining) sRemaining
from
SaleInvoices
group by YEAR(Date),DATEPART(MONTH,Date)
order by YEAR(Date),DATEPART(MONTH,Date)
( #RMB without null but sometimes #RMB Month Column Value and #RMS Month Column value is Different )
create table #RMB
(
[Year] int,
[Month] int,
bTAccount bigint,
bRemaining bigint
)
insert into #RMB(Year,Month,bTAccount,bRemaining)
select
YEAR(Date) [Year],
DATEPART(MONTH,Date) [Month],
sum(TAccount) bTAccount,
sum(Remaining) bRemaining
from
BuyInvoices
group by YEAR(Date),DATEPART(MONTH,Date)
order by YEAR(Date),DATEPART(MONTH,Date)
select * from #RMS
Full Outer Join #RMB
on #RMS.Year=#RMB.Year and #RMS.Month=#RMB.Month
group by #RMS.Year, #RMS.Month
order by #RMS.Year, #RMS.Month
感谢您的回答
您的 SELECT
列表有误。将 *
替换为 #RMS.*, #RMB.*
或(更好)您想要的字段的明确列表,在它们的名称前加上它们来自的 table 的名称。这也允许不重复您加入的字段。