如何从派生 Table 或子查询中提取列

How To Pull In Columns From A Derived Table Or Sub Query

我有一个查询,用于查找没有匹配帐号的记录并尝试按地址匹配这些帐户。

我得到了我想要的结果,但我想包括下面表 2 中的列。我该怎么做?

Select DISTINCT
              account_num
        ,product
        ,accountName
        ,address_1
        ,address_2
        ,city
        ,state
        ,zip
        ,short_address
INTO #Matching_Address
From #Non_Matching_Accounts t
Where EXISTS 
(SELECT * FROM (SELECT 
                       left(ADDRESS_LINE1_TXT,20) AS matching_add 
                      ,CITY
                      ,STATE
                      ,ZIP
                      ,ACCOUNT_OWNER
               From [database].[dbo].[table2]) v (matching_add, CITY, STATE,ZIP,ACCOUNT_OWNER)
               WHERE 
               t.short_address= v.matching_add 
               AND t.city= v.NAME
               AND t.state = v.STATE
               AND t.zip = v.ZIP
               AND t.accountName LIKE '%'+v.ACCOUNT_OWNER+'%')

我试过:

Select DISTINCT
              account_num
        ,product
        ,accountName
        ,address_1
        ,address_2
        ,city
        ,state
        ,zip
        ,short_address
              ,matching_add 
              ,CITY
        ,STATE
        ,ZIP
        ,ACCOUNT_OWNER
INTO #Matching_Address
From #Non_Matching_Accounts t
Where EXISTS 
(SELECT * FROM (SELECT 
                       left(ADDRESS_LINE1_TXT,20) AS Select DISTINCT
              account_num
        ,product
        ,accountName
        ,address_1
        ,address_2
        ,city
        ,state
        ,zip
        ,short_address
INTO #Matching_Address
From #Non_Matching_Accounts t
Where EXISTS 
(SELECT * FROM (SELECT 
                       left(ADDRESS_LINE1_TXT,20) AS matching_add 
                      ,CITY
                      ,STATE
                      ,ZIP
                      ,ACCOUNT_OWNER
               From [database].[dbo].[table2]) v (matching_add, CITY, STATE,ZIP,ACCOUNT_OWNER)
               WHERE 
               t.short_address= v.matching_add 
               AND t.city= v.NAME
               AND t.state = v.STATE
               AND t.zip = v.ZIP
               AND t.accountName LIKE '%'+v.ACCOUNT_OWNER+'%')
               From [database].[dbo].[table2]) v (matching_add, CITY, STATE,ZIP,ACCOUNT_OWNER)
               WHERE 
               t.short_address= v.matching_add 
               AND t.city= v.NAME
               AND t.state = v.STATE
               AND t.zip = v.ZIP
               AND t.accountName LIKE '%'+v.ACCOUNT_OWNER+'%')

预期结果:

acct_num|prd|actName|add1|add2|city|state|zip|act_num2|prd2|actName|add1|add2|city2|state2|zip2|
----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
a   |  a  |  a |  a  |  a | a | a  | a   | a   | a   | a   | a  | a  | a    a|  a 
b   |  b  |  b |  b  |  b |  b  |  b |  b  |  b | b   |  b  |  b |  b  |  b |  b  
c   |  c  |  c |  c  |  c | c  |  c  |  c |  c  |  c | c  |  c |  c  |  c |  c |
d   |  d  |  d |  d  |  d |  d  |  d |  d  |  d |  d  |  d |  d  |  d | d |  d  |

当建议使用 'inner join' 时,您正在使用 'exists'。重组如下:

select 
distinct  t.account_num,
          t.product,
          t.accountName,
          t.address_1,
          t.address_2,
          t.city,
          t.state,
          t.zip,
          t.short_address,

          matching_add = left(v.address_line1_txt,20),
          vCity = v.city,
          vState = v.state,
          vZip = v.zip,
          v.account_owner

into      #Matching_Address
from      #Non_Matching_Accounts t
join      [database].[dbo].[table2] v
              on  t.short_address = v.matching_add 
              and t.city = v.name
              and t.state = v.state
              and t.zip = v.zip
              and t.accountName like '%' + v.account_owner + '%'

内部联接(或简称 'join')只会匹配 return,因此在这个意义上它的工作方式类似于 'exists'。但它使右侧 table 中的列可供您使用。

我的直觉是你可能已经尝试过这个。我在您的查询中看到了一个 'distinct',如果只使用 'exists',这可能是不必要的。您放弃 'inner join' 是因为它重复了您的行吗?如果是这样,'exists' 仍然不是答案。也许交叉应用可以帮助你:

select       ... (same as above)
into         #Matching_Address
from         #Non_Matching_Accounts t
cross apply  (
                select 
                top 1     *
                from      [database].[dbo].[table2] v
                where     t.short_address = v.matching_add 
                and       t.city = v.name
                and       t.state = v.state
                and       t.zip = v.zip
                and       t.accountName like '%' + v.account_owner + '%'
                order by  v.matching_add -- or whatever puts the better one on top
             ) v

使用 'top 1','v' 结果将在 't' 中每行产生不超过 1 条记录。使用'cross apply',如果'v'的结果是没有记录,那么't'就不会return一行,(类似于'exists'或'inner join' ).