如果存在则检查多列中的值,然后在SQL 2008中更新Row-

时间:2017-02-17 16:39:51

标签: sql sql-server

我有一张包含以下数据的表格。

我正在尝试根据以下规则更新丢失的Relation Number记录:

  • 当两行POSTCODE相同时

  • 如果Phone1phone2phone3匹配,或Account name匹配(帐户名%)

表:

DECLARE @MASTER TABLE
(
[Account Number] VARCHAR(8),    
[Account Name] VARCHAR (100),   
RelationNo VARCHAR(8),  
BRNAME VARCHAR(40), 
POSTCODE  VARCHAR(10),
PHONE001 VARCHAR(250),
PHONE002 VARCHAR(250),
PHONE003  VARCHAR(250)
)

INSERT @MASTER


SELECT '40045002', 'REVIVE', '', '2', 'WA168QE', '', '', '' UNION ALL -- Account Name Match
SELECT '60045002', 'REVIVE! MANCHESTER', '120002', '2', 'WA168QE', '07967967404', '', ''  UNION ALL -- Account Name Match


SELECT 'C2128617', 'KGN ELECTRIC', '', '85', 'RH54JH', '', '07773886450', '02072542392' UNION ALL -- Phone  match
SELECT 'D2128617',  'J & M SERVICES', '121542', '9', 'RH54JH', '', '', '07773886450' UNION ALL -- Phone  match


SELECT 'E3085970', 'RICHARDS AUTO SERVICES', '122390', '212','SY120EW', '01691622421',  NULL, NULL UNION ALL -- Not To be in Output Nothing Match
SELECT 'T5000647', 'Thomas Halsall-mooney', '', '212', 'SY120EW', '07493580779',  NULL,  NULL  -- Not To be in Output Nothing Match



 Output
'40045002',  'REVIVE', '120002', '2', 'WA168QE', '', '', ''  -- Account Name Match
'60045002', 'REVIVE! MANCHESTER', '120002', '2', 'WA168QE', '07967967404', '', ''   -- Account Name Match
'C2128617', 'KGN ELECTRIC', '121542', '85', 'RH54JH', '', '07773886450', '02072542392'  -- Phone  match
'D2128617', 'J & M SERVICES', '121542', '9', 'RH54JH', '', ''  ,'07773886450'  -- Phone  match

然而,代码并没有给出完美的结果。

;with cte as (
select o.*, x.FoundRelationNo
from @MASTER as o
cross apply (
  select top 1 FoundRelationNo = i.RelationNo 
    from @MASTER  as i
    where i.POSTCODE = o.POSTCODE
      and nullif(i.RelationNo,'') is not null
      and (
        nullif(i.PHONE001,'0') in (nullif(o.PHONE001,'0'), nullif(o.PHONE002,'0'),nullif(o.PHONE003,'0'))
      or nullif(i.PHONE002,'0') in (nullif(o.PHONE001,'0'), nullif(o.PHONE002,'0'),nullif(o.PHONE003,'0'))
      or i.[Account Name] like o.[Account Name]+'%')

    ) as x
where nullif(o.RelationNo,'') is null
 and (o.PHONE001 > '0' or o.PHONE001 > '0' or o.PHONE001>'0')
 )

 update cte set RelationNo = FoundRelationNo

请帮帮我。

1 个答案:

答案 0 :(得分:0)

rextester:http://rextester.com/WEQ75349

;with cte as (
select o.*, x.*
from @MASTER as o
cross apply (
  select top 1 
      MatchedRelationNo = i.RelationNo 
    , MatchedAccountNumber = i.[Account Number]
    , MatchedOn = case 
        when i.[Account Name] like o.[Account Name]+'%'
          then 'Account Name' 
        when nullif(i.PHONE001,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
          then 'Phone001'
        when nullif(i.PHONE002,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
          then 'Phone002'
        when nullif(i.PHONE003,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
          then 'Phone003'
        else null
        end
    , MatchedAccountName =  case 
        when i.[Account Name] like o.[Account Name]+'%'
          then i.[Account Name]
        else null 
        end
    , MatchedPhone = case 
        when nullif(i.PHONE001,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
          then i.PHONE001
        when nullif(i.PHONE002,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
          then i.PHONE002
        when nullif(i.PHONE003,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
          then i.PHONE003
        else null
        end
    from @MASTER  as i
    where i.POSTCODE = o.POSTCODE
      and nullif(i.RelationNo,'') is not null
      and (
         nullif(i.PHONE001,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
      or nullif(i.PHONE002,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
      or nullif(i.PHONE003,'') in (o.PHONE001, o.PHONE002,o.PHONE003)
      or i.[Account Name] like o.[Account Name]+'%'
       )
   ) as x
where nullif(o.RelationNo,'') is null
 )
select * from cte
--update cte set RelationNo = MatchedRelationNo
--select * from @Master

结果:

+----------------+--------------+------------+--------+----------+----------+-------------+-------------+-------------------+----------------------+--------------+--------------------+--------------+
| Account Number | Account Name | RelationNo | BRNAME | POSTCODE | PHONE001 |  PHONE002   |  PHONE003   | MatchedRelationNo | MatchedAccountNumber |  MatchedOn   | MatchedAccountName | MatchedPhone |
+----------------+--------------+------------+--------+----------+----------+-------------+-------------+-------------------+----------------------+--------------+--------------------+--------------+
| 40045002       | REVIVE       |            |      2 | WA168QE  |          |             |             |            120002 | 60045002             | Account Name | REVIVE! MANCHESTER | NULL         |
| C2128617       | KGN ELECTRIC |            |     85 | RH54JH   |          | 07773886450 | 02072542392 |            121542 | D2128617             | Phone003     | NULL               | 07773886450  |
+----------------+--------------+------------+--------+----------+----------+-------------+-------------+-------------------+----------------------+--------------+--------------------+--------------+