如何加入2个临时表?

时间:2012-05-16 18:03:39

标签: sql join temp-tables

我创建了2个临时表,但我遇到了加入它们的问题。

表#1

Create Table #First_Pay(
SAID int,
First_Payment date)
select b.CUSTNMBR, min(b.docdate) as first_payment
from RM20101 b
where b.CUSTNMBR = '1973204005'
and b.CHEKNMBR > '1'
Group by b.CUSTNMBR

表#2

Create Table #First_Bil(
SAID int,
First_Bill date)
Select a.CUSTNMBR, MIN(a.Tax_Date) as First_Bill
from SOP30200 as a
where a.CUSTNMBR = '1973204005'
Group by a.CUSTNMBR

我使用了这个查询:

Select a.SAID, a.First_Bill, b.First_Payment
From #First_Bil a 
Full Join
#First_Pay b
On a.SAID = b.SAID;


drop table #First_Bil
drop table #First_Pay

但我得到了空白。我做错了什么?

2 个答案:

答案 0 :(得分:1)

您不必在创建临时表后选择数据,而是必须将其插入临时表:

Create Table #First_Pay(SAID int,
                        First_Payment date)

insert into #First_Pay select b.CUSTNMBR, min(b.docdate) as first_payment
                       from RM20101 b
                       where b.CUSTNMBR = '1973204005'
                       and b.CHEKNMBR > '1'
                       Group by b.CUSTNMBR

Create Table #First_Bil(SAID int,
                        First_Bill date)

insert into #First_Bil Select a.CUSTNMBR, MIN(a.Tax_Date) as First_Bill
                       from SOP30200 as a
                       where a.CUSTNMBR = '1973204005'
                       Group by a.CUSTNMBR

答案 1 :(得分:0)

我假设你的表中有数据?你创建它们后没有插入任何地方?确保有一个插入行