У меня есть запрос, который ищет записи, у которых нет соответствующего номера счета, и пытается сопоставить эти учетные записи по адресу.
Я получаю результаты, которые хочу, но хочу включить столбцы изтаблица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 |