【问题标题】:How to join 2 temp tables?如何加入 2 个临时表?
【发布时间】:2012-05-24 08:17:00
【问题描述】:

我创建了 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

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

【问题讨论】:

  • 您在临时表中的何处插入值?
  • 您似乎忘记INSERT 表中的任何数据。

标签: sql join temp-tables


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

    • 你太棒了,先生,这样的事情让我觉得自己像个白痴
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-13
    • 2021-12-08
    • 2014-07-16
    相关资源
    最近更新 更多