如何从派生表或子查询中拉入列

时间:2019-02-05 23:01:40

标签: sql sql-server subquery exists derived-table

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

我得到了想要的结果,但是我想包括下表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  |

1 个答案:

答案 0 :(得分:1)

当建议“内部联接”时,您正在使用“存在”。重组如下:

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”)将仅返回匹配项,因此在这种意义上它的作用类似于“存在”。但是,它使您可以使用右侧表中的列。

我的直觉是您可能已经尝试过了。我在您的查询中看到一个“与众不同”,可能仅凭“存在”就没有必要。您是否因为内部重复而放弃了“内部联接”?如果是这样,“存在”仍然不是答案。也许交叉申请可以帮助您:

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条。使用“交叉应用”,如果“ v”的结果为无记录,则“ t”将不返回行(类似于“ exists”或“ inner join”)。