加入两个临时表与完全外部加入

时间:2017-02-11 05:35:35

标签: sql full-outer-join

我希望使用Full outer join连接两个临时表但不能正常工作 并且总是只显示没有#RMB !!的#RMS值

此代码中的位置和错误在哪里?

(#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没有null但有时#RMB月份列值和#RMS月份列值不同)

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

感谢您的回答

1 个答案:

答案 0 :(得分:0)

您的const char* string = "Test"; char* const string = "Test"; const char* const string = "Test"; 列表错误。将SELECT替换为*或(更好)显示所需字段的明确列表,在其名称前面加上它们来自的表的名称。这也允许不重复您加入的字段。