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

问题描述 投票:0回答:1

我有一个查询,查找没有匹配的帐户号码记录,并试图通过地址匹配这些帐户。

我得到我想要的结果,但我想包括从下面的表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  |
sql sql-server subquery exists derived-table
1个回答
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 + '%'

内部连接(或只是“加入”的简称),将只返回匹配,所以它在这个意义上说像“存在”。但它使从右侧表中的列可用。

我的直觉是,你可能已经尝试过这一点。我看到您的查询,这可能不会是必要的只是“存在”一“不同”。你放弃“内部联接”,因为复制您的行?如果是这样,“存在”仍然没有答案。也许一个跨应用可以帮助你:

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

随着“顶1”,在“V”的结果会产生每行不超过1记录在“T”。随着“跨适用”,如果“V”的结果是没有记录,那么“T”不会返回一行,(类似于“存在”或“内部联接”)。

© www.soinside.com 2019 - 2024. All rights reserved.