2012-05-16 79 views
2

我創建了2個臨時表,並且遇到了連接它們的問題。 表#1SQL:如何加入2個臨時表?

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 

,但我得到的空白我究竟做錯了,爲什麼會這樣HARD ?

+0

你在哪裏插入臨時表中的值? –

+0

您似乎忘記了將任何數據「INSERT」到表中。 –

回答

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 
+0

你真棒,先生,這樣的事情讓我覺得自己像個白癡 –

0

我假設你有你的表中的數據?創建它們後,你不會插入任何地方?確保有一個插入行